1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-06 11:56:46 +01:00

chore(server): better ML error messages (#5914)

Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
This commit is contained in:
Alex 2024-01-04 14:34:50 -06:00 committed by GitHub
parent 4f6f79a392
commit 13ba83dce6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -12,16 +12,20 @@ import {
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { readFile } from 'fs/promises'; import { readFile } from 'fs/promises';
const errorPrefix = 'Machine learning request';
@Injectable() @Injectable()
export class MachineLearningRepository implements IMachineLearningRepository { export class MachineLearningRepository implements IMachineLearningRepository {
private async post<T>(url: string, input: TextModelInput | VisionModelInput, config: ModelConfig): Promise<T> { private async post<T>(url: string, input: TextModelInput | VisionModelInput, config: ModelConfig): Promise<T> {
const formData = await this.getFormData(input, config); const formData = await this.getFormData(input, config);
const res = await fetch(`${url}/predict`, { method: 'POST', body: formData });
const res = await fetch(`${url}/predict`, { method: 'POST', body: formData }).catch((error: Error | any) => {
throw new Error(`${errorPrefix} to "${url}" failed with ${error?.cause || error}`);
});
if (res.status >= 400) { if (res.status >= 400) {
throw new Error( const modelType = config.modelType ? ` for ${config.modelType.replace('-', ' ')}` : '';
`Request ${config.modelType ? `for ${config.modelType.replace('-', ' ')} ` : ''}` + throw new Error(`${errorPrefix}${modelType} failed with status ${res.status}: ${res.statusText}`);
`failed with status ${res.status}: ${res.statusText}`,
);
} }
return res.json(); return res.json();
} }