diff --git a/backend/src/libs/packages/api/base-http-api.ts b/backend/src/libs/packages/api/base-http-api.ts index 70f428463..b89943087 100644 --- a/backend/src/libs/packages/api/base-http-api.ts +++ b/backend/src/libs/packages/api/base-http-api.ts @@ -6,7 +6,7 @@ import { HTTPHeader, } from '#libs/packages/http/http.js'; import { HTTPError } from '#libs/packages/http/http.js'; -import { type ServerErrorResponse, type ValueOf } from '#libs/types/types.js'; +import { type ValueOf } from '#libs/types/types.js'; import { type HTTPApi, type HTTPApiOptions } from './libs/types/types.js'; @@ -83,17 +83,11 @@ class BaseHttpApi implements HTTPApi { return response; } - private async handleError(response: Response): Promise { - let parsedException: ServerErrorResponse; - - try { - parsedException = (await response.json()) as ServerErrorResponse; - } catch { - parsedException = { - errorType: ServerErrorType.COMMON, - message: response.statusText, - }; - } + private handleError(response: Response): Promise { + const parsedException = { + errorType: ServerErrorType.COMMON, + message: response.statusText, + }; throw new HTTPError({ status: response.status as ValueOf, diff --git a/backend/src/libs/packages/open-ai/open-ai.package.ts b/backend/src/libs/packages/open-ai/open-ai.package.ts index 7aa301fc1..741fa8c1c 100644 --- a/backend/src/libs/packages/open-ai/open-ai.package.ts +++ b/backend/src/libs/packages/open-ai/open-ai.package.ts @@ -1,8 +1,9 @@ import { ContentType } from '#libs/enums/enums.js'; +import { ChatError } from '#libs/exceptions/exceptions.js'; import { type ValueOf } from '#libs/types/types.js'; import { BaseHttpApi } from '../api/api.js'; -import { type BaseHttp } from '../http/http.js'; +import { type BaseHttp, HTTPCode, HTTPError } from '../http/http.js'; import { IMAGE_SIZE } from './libs/constants/constants.js'; import { OpenAiApiPath, @@ -48,26 +49,30 @@ class OpenAi extends BaseHttpApi { public async getMessageResponse( messages: OpenAiMessageGenerateRequestDto[], ): Promise { - const data = await this.load( - this.getFullEndpoint( - OpenAiApiPath.CHAT, - OpenAiChatApiPath.COMPLETIONS, - {}, - ), - { - method: 'POST', - payload: JSON.stringify({ - model: this.model, - messages, - }), - token: this.apiKey, - contentType: ContentType.JSON, - }, - ); + try { + const data = await this.load( + this.getFullEndpoint( + OpenAiApiPath.CHAT, + OpenAiChatApiPath.COMPLETIONS, + {}, + ), + { + method: 'POST', + payload: JSON.stringify({ + model: this.model, + messages, + }), + token: this.apiKey, + contentType: ContentType.JSON, + }, + ); - const [response] = data.choices; + const [response] = data.choices; - return response?.message.content ?? null; + return response?.message.content ?? null; + } catch (error: unknown) { + this.throwError(error); + } } public async generateImages({ @@ -75,28 +80,47 @@ class OpenAi extends BaseHttpApi { number = this.defaultImageGenerateConfig.number, size = this.defaultImageGenerateConfig.size, }: OpenAiImageGenerateRequestDto): Promise { - const data = await this.load( - this.getFullEndpoint( - OpenAiApiPath.IMAGES, - OpenAiImagesApiPath.GENERATIONS, - {}, - ), - { - method: 'POST', - payload: JSON.stringify({ - prompt, - n: number, - size, - response_format: this.defaultImageGenerateConfig.responseFormat, - }), - token: this.apiKey, - contentType: ContentType.JSON, - }, - ); + try { + const data = await this.load( + this.getFullEndpoint( + OpenAiApiPath.IMAGES, + OpenAiImagesApiPath.GENERATIONS, + {}, + ), + { + method: 'POST', + payload: JSON.stringify({ + prompt, + n: number, + size, + response_format: this.defaultImageGenerateConfig.responseFormat, + }), + token: this.apiKey, + contentType: ContentType.JSON, + }, + ); - const [response] = data.data; + const [response] = data.data; - return response?.b64_json ?? null; + return response?.b64_json ?? null; + } catch (error: unknown) { + this.throwError(error); + } + } + + private throwError(error: unknown): never { + if (error instanceof HTTPError) { + if (error.status === HTTPCode.UNAUTHORIZED) { + throw new ChatError({ + message: error.message, + status: HTTPCode.BAD_REQUEST, + }); + } + + throw new ChatError({ message: error.message, status: error.status }); + } + + throw new ChatError({}); } }