diff --git a/apps/chat/src/pages/api/publication/approve.ts b/apps/chat/src/pages/api/publication/approve.ts index 9db4ab155c..ae7322894c 100644 --- a/apps/chat/src/pages/api/publication/approve.ts +++ b/apps/chat/src/pages/api/publication/approve.ts @@ -5,6 +5,7 @@ import { getToken } from 'next-auth/jwt'; import { validateServerSession } from '@/src/utils/auth/session'; import { getApiHeaders } from '@/src/utils/server/get-headers'; import { logger } from '@/src/utils/server/logger'; +import { ServerUtils } from '@/src/utils/server/server'; import { DialAIError } from '@/src/types/error'; @@ -36,11 +37,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { let json: unknown; if (!proxyRes.ok) { - try { - json = await proxyRes.json(); - } catch (err) { - json = undefined; - } + json = await ServerUtils.getErrorMessageFromResponse(proxyRes); throw new DialAIError( (typeof json === 'string' && json) || proxyRes.statusText, diff --git a/apps/chat/src/pages/api/publication/create.ts b/apps/chat/src/pages/api/publication/create.ts index e744443630..9e925dd32a 100644 --- a/apps/chat/src/pages/api/publication/create.ts +++ b/apps/chat/src/pages/api/publication/create.ts @@ -5,6 +5,7 @@ import { getToken } from 'next-auth/jwt'; import { validateServerSession } from '@/src/utils/auth/session'; import { getApiHeaders } from '@/src/utils/server/get-headers'; import { logger } from '@/src/utils/server/logger'; +import { ServerUtils } from '@/src/utils/server/server'; import { DialAIError } from '@/src/types/error'; @@ -36,11 +37,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { let json: unknown; if (!proxyRes.ok) { - try { - json = await proxyRes.json(); - } catch (err) { - json = undefined; - } + json = await ServerUtils.getErrorMessageFromResponse(proxyRes); throw new DialAIError( (typeof json === 'string' && json) || proxyRes.statusText, diff --git a/apps/chat/src/pages/api/publication/rulesList.ts b/apps/chat/src/pages/api/publication/rulesList.ts index f7bd0b4ff9..7b889ae707 100644 --- a/apps/chat/src/pages/api/publication/rulesList.ts +++ b/apps/chat/src/pages/api/publication/rulesList.ts @@ -5,6 +5,7 @@ import { getToken } from 'next-auth/jwt'; import { validateServerSession } from '@/src/utils/auth/session'; import { getApiHeaders } from '@/src/utils/server/get-headers'; import { logger } from '@/src/utils/server/logger'; +import { ServerUtils } from '@/src/utils/server/server'; import { DialAIError } from '@/src/types/error'; @@ -36,11 +37,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { let json: unknown; if (!proxyRes.ok) { - try { - json = await proxyRes.json(); - } catch (err) { - json = undefined; - } + json = await ServerUtils.getErrorMessageFromResponse(proxyRes); throw new DialAIError( (typeof json === 'string' && json) || proxyRes.statusText, diff --git a/apps/chat/src/store/publication/publication.epics.ts b/apps/chat/src/store/publication/publication.epics.ts index a990a5506a..d35000b6da 100644 --- a/apps/chat/src/store/publication/publication.epics.ts +++ b/apps/chat/src/store/publication/publication.epics.ts @@ -97,7 +97,7 @@ const publishEpic: AppEpic = (action$) => switchMap(() => EMPTY), catchError((err) => { console.error(err); - return of(PublicationActions.publishFail()); + return of(PublicationActions.publishFail(err.message)); }), ); }), @@ -106,8 +106,10 @@ const publishEpic: AppEpic = (action$) => const publishFailEpic: AppEpic = (action$) => action$.pipe( filter(PublicationActions.publishFail.match), - map(() => - UIActions.showErrorToast(translate(errorsMessages.publicationFailed)), + map(({ payload }) => + UIActions.showErrorToast( + translate(payload ?? errorsMessages.publicationFailed), + ), ), ); @@ -813,7 +815,7 @@ const approvePublicationEpic: AppEpic = (action$, state$) => }), catchError((err) => { console.error(err); - return of(PublicationActions.approvePublicationFail()); + return of(PublicationActions.approvePublicationFail(err.message)); }), ), ), @@ -822,9 +824,9 @@ const approvePublicationEpic: AppEpic = (action$, state$) => const approvePublicationFailEpic: AppEpic = (action$) => action$.pipe( filter(PublicationActions.approvePublicationFail.match), - map(() => + map(({ payload }) => UIActions.showErrorToast( - translate(errorsMessages.publicationApproveFailed), + translate(payload ?? errorsMessages.publicationApproveFailed), ), ), ); @@ -907,7 +909,7 @@ const uploadRulesEpic: AppEpic = (action$) => }), catchError((err) => { console.error(err); - return of(PublicationActions.uploadRulesFail()); + return of(PublicationActions.uploadRulesFail(err.message)); }), ), ), @@ -916,8 +918,10 @@ const uploadRulesEpic: AppEpic = (action$) => const uploadRulesFailEpic: AppEpic = (action$) => action$.pipe( filter(PublicationActions.uploadRulesFail.match), - map(() => - UIActions.showErrorToast(translate(errorsMessages.rulesUploadingFailed)), + map(({ payload }) => + UIActions.showErrorToast( + translate(payload ?? errorsMessages.rulesUploadingFailed), + ), ), ); diff --git a/apps/chat/src/utils/server/api.ts b/apps/chat/src/utils/server/api.ts index 4d891254b0..bc60514471 100644 --- a/apps/chat/src/utils/server/api.ts +++ b/apps/chat/src/utils/server/api.ts @@ -1,4 +1,4 @@ -import { Observable, from, switchMap, throwError } from 'rxjs'; +import { Observable, catchError, from, switchMap, throwError } from 'rxjs'; import { fromFetch } from 'rxjs/fetch'; import { Conversation, ConversationInfo } from '@/src/types/chat'; @@ -107,7 +107,12 @@ export class ApiUtils { }).pipe( switchMap((response) => { if (!response.ok) { - return throwError(() => new Error(response.status + '')); + return from(response.text()).pipe( + switchMap((errorMessage) => + throwError(() => new Error(errorMessage)), + ), + catchError(() => throwError(() => new Error(response.status + ''))), + ); } return from(response.json()); diff --git a/apps/chat/src/utils/server/server.ts b/apps/chat/src/utils/server/server.ts index 4cb03a61e7..e48aee7ba8 100644 --- a/apps/chat/src/utils/server/server.ts +++ b/apps/chat/src/utils/server/server.ts @@ -3,6 +3,8 @@ import { NextApiRequest } from 'next'; import { constructPath } from '../app/file'; import { ApiUtils } from './api'; +import { Response } from 'node-fetch'; + export class ServerUtils { public static getEntityTypeFromPath = ( req: NextApiRequest, @@ -16,4 +18,18 @@ export class ServerUtils { .filter(Boolean) .map((part) => ApiUtils.safeEncodeURIComponent(part as string)), ); + + public static getErrorMessageFromResponse = async ( + res: Response, + ): Promise => { + try { + return (await res.json()) as string; + } catch { + try { + return await res?.text(); + } catch { + return null; + } + } + }; }