From a0e289aef6df8db8c99b8464a9cc4dbad8a84d62 Mon Sep 17 00:00:00 2001 From: Magomed-Elbi Dzhukalaev Date: Mon, 29 Jul 2024 18:35:24 +0300 Subject: [PATCH 1/5] fix(chat): add error messages for failed publish requests --- .../chat/src/pages/api/publication/approve.ts | 7 ++---- apps/chat/src/pages/api/publication/create.ts | 7 ++---- .../src/pages/api/publication/rulesList.ts | 7 ++---- .../store/publication/publication.epics.ts | 22 +++++++++++-------- apps/chat/src/utils/server/api.ts | 8 +++++-- .../utils/server/get-response-error-msg.ts | 18 +++++++++++++++ 6 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 apps/chat/src/utils/server/get-response-error-msg.ts diff --git a/apps/chat/src/pages/api/publication/approve.ts b/apps/chat/src/pages/api/publication/approve.ts index 9db4ab155c..b39f3776a0 100644 --- a/apps/chat/src/pages/api/publication/approve.ts +++ b/apps/chat/src/pages/api/publication/approve.ts @@ -4,6 +4,7 @@ import { getToken } from 'next-auth/jwt'; import { validateServerSession } from '@/src/utils/auth/session'; import { getApiHeaders } from '@/src/utils/server/get-headers'; +import { getResponseErrorMsg } from '@/src/utils/server/get-response-error-msg'; import { logger } from '@/src/utils/server/logger'; 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 getResponseErrorMsg(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 c377e7974f..a83bfe8bd6 100644 --- a/apps/chat/src/pages/api/publication/create.ts +++ b/apps/chat/src/pages/api/publication/create.ts @@ -4,6 +4,7 @@ import { getToken } from 'next-auth/jwt'; import { validateServerSession } from '@/src/utils/auth/session'; import { getApiHeaders } from '@/src/utils/server/get-headers'; +import { getResponseErrorMsg } from '@/src/utils/server/get-response-error-msg'; import { logger } from '@/src/utils/server/logger'; import { DialAIError } from '@/src/types/error'; @@ -39,11 +40,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 getResponseErrorMsg(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..92bde175c0 100644 --- a/apps/chat/src/pages/api/publication/rulesList.ts +++ b/apps/chat/src/pages/api/publication/rulesList.ts @@ -4,6 +4,7 @@ import { getToken } from 'next-auth/jwt'; import { validateServerSession } from '@/src/utils/auth/session'; import { getApiHeaders } from '@/src/utils/server/get-headers'; +import { getResponseErrorMsg } from '@/src/utils/server/get-response-error-msg'; import { logger } from '@/src/utils/server/logger'; 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 getResponseErrorMsg(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 8d8d8f3323..888686375a 100644 --- a/apps/chat/src/store/publication/publication.epics.ts +++ b/apps/chat/src/store/publication/publication.epics.ts @@ -99,7 +99,7 @@ const publishEpic: AppEpic = (action$) => switchMap(() => EMPTY), catchError((err) => { console.error(err); - return of(PublicationActions.publishFail()); + return of(PublicationActions.publishFail(err.message)); }), ); }), @@ -108,8 +108,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), + ), ), ); @@ -865,7 +867,7 @@ const approvePublicationEpic: AppEpic = (action$, state$) => }), catchError((err) => { console.error(err); - return of(PublicationActions.approvePublicationFail()); + return of(PublicationActions.approvePublicationFail(err.message)); }), ), ), @@ -874,9 +876,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), ), ), ); @@ -959,7 +961,7 @@ const uploadRulesEpic: AppEpic = (action$) => }), catchError((err) => { console.error(err); - return of(PublicationActions.uploadRulesFail()); + return of(PublicationActions.uploadRulesFail(err.message)); }), ), ), @@ -968,8 +970,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..553c78110d 100644 --- a/apps/chat/src/utils/server/api.ts +++ b/apps/chat/src/utils/server/api.ts @@ -107,10 +107,14 @@ 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)), + ), + ); } - return from(response.json()); + return response.json(); }), ); } diff --git a/apps/chat/src/utils/server/get-response-error-msg.ts b/apps/chat/src/utils/server/get-response-error-msg.ts new file mode 100644 index 0000000000..c178520292 --- /dev/null +++ b/apps/chat/src/utils/server/get-response-error-msg.ts @@ -0,0 +1,18 @@ +import { Response } from 'node-fetch'; + +export async function getResponseErrorMsg(res: Response) { + let resBody: unknown; + let msg: unknown; + try { + resBody = await res?.text(); + } catch (err) { + resBody = undefined; + } + try { + msg = await res.json(); + } catch (err) { + msg = resBody; + } + + return msg; +} From 17d4715fd184723431259a37df03c66ae8f876e2 Mon Sep 17 00:00:00 2001 From: Magomed-Elbi Dzhukalaev Date: Wed, 31 Jul 2024 12:55:45 +0300 Subject: [PATCH 2/5] fix(chat): fix request response wrapper --- apps/chat/src/utils/server/api.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/chat/src/utils/server/api.ts b/apps/chat/src/utils/server/api.ts index 553c78110d..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'; @@ -111,10 +111,11 @@ export class ApiUtils { switchMap((errorMessage) => throwError(() => new Error(errorMessage)), ), + catchError(() => throwError(() => new Error(response.status + ''))), ); } - return response.json(); + return from(response.json()); }), ); } From eb46fe368e2ea70e5c57269365be093c7864b5f1 Mon Sep 17 00:00:00 2001 From: Magomed-Elbi Dzhukalaev Date: Wed, 31 Jul 2024 12:56:49 +0300 Subject: [PATCH 3/5] fix(chat): move error parser to ServerUtils --- .../chat/src/pages/api/publication/approve.ts | 4 ++-- apps/chat/src/pages/api/publication/create.ts | 4 ++-- .../src/pages/api/publication/rulesList.ts | 4 ++-- .../utils/server/get-response-error-msg.ts | 18 ---------------- apps/chat/src/utils/server/server.ts | 21 +++++++++++++++++++ 5 files changed, 27 insertions(+), 24 deletions(-) delete mode 100644 apps/chat/src/utils/server/get-response-error-msg.ts diff --git a/apps/chat/src/pages/api/publication/approve.ts b/apps/chat/src/pages/api/publication/approve.ts index b39f3776a0..ae7322894c 100644 --- a/apps/chat/src/pages/api/publication/approve.ts +++ b/apps/chat/src/pages/api/publication/approve.ts @@ -4,8 +4,8 @@ import { getToken } from 'next-auth/jwt'; import { validateServerSession } from '@/src/utils/auth/session'; import { getApiHeaders } from '@/src/utils/server/get-headers'; -import { getResponseErrorMsg } from '@/src/utils/server/get-response-error-msg'; import { logger } from '@/src/utils/server/logger'; +import { ServerUtils } from '@/src/utils/server/server'; import { DialAIError } from '@/src/types/error'; @@ -37,7 +37,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { let json: unknown; if (!proxyRes.ok) { - json = await getResponseErrorMsg(proxyRes); + 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 92b2d63274..9e925dd32a 100644 --- a/apps/chat/src/pages/api/publication/create.ts +++ b/apps/chat/src/pages/api/publication/create.ts @@ -4,8 +4,8 @@ import { getToken } from 'next-auth/jwt'; import { validateServerSession } from '@/src/utils/auth/session'; import { getApiHeaders } from '@/src/utils/server/get-headers'; -import { getResponseErrorMsg } from '@/src/utils/server/get-response-error-msg'; import { logger } from '@/src/utils/server/logger'; +import { ServerUtils } from '@/src/utils/server/server'; import { DialAIError } from '@/src/types/error'; @@ -37,7 +37,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { let json: unknown; if (!proxyRes.ok) { - json = await getResponseErrorMsg(proxyRes); + 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 92bde175c0..7b889ae707 100644 --- a/apps/chat/src/pages/api/publication/rulesList.ts +++ b/apps/chat/src/pages/api/publication/rulesList.ts @@ -4,8 +4,8 @@ import { getToken } from 'next-auth/jwt'; import { validateServerSession } from '@/src/utils/auth/session'; import { getApiHeaders } from '@/src/utils/server/get-headers'; -import { getResponseErrorMsg } from '@/src/utils/server/get-response-error-msg'; import { logger } from '@/src/utils/server/logger'; +import { ServerUtils } from '@/src/utils/server/server'; import { DialAIError } from '@/src/types/error'; @@ -37,7 +37,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { let json: unknown; if (!proxyRes.ok) { - json = await getResponseErrorMsg(proxyRes); + json = await ServerUtils.getErrorMessageFromResponse(proxyRes); throw new DialAIError( (typeof json === 'string' && json) || proxyRes.statusText, diff --git a/apps/chat/src/utils/server/get-response-error-msg.ts b/apps/chat/src/utils/server/get-response-error-msg.ts deleted file mode 100644 index c178520292..0000000000 --- a/apps/chat/src/utils/server/get-response-error-msg.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Response } from 'node-fetch'; - -export async function getResponseErrorMsg(res: Response) { - let resBody: unknown; - let msg: unknown; - try { - resBody = await res?.text(); - } catch (err) { - resBody = undefined; - } - try { - msg = await res.json(); - } catch (err) { - msg = resBody; - } - - return msg; -} diff --git a/apps/chat/src/utils/server/server.ts b/apps/chat/src/utils/server/server.ts index 4cb03a61e7..94d3baed9b 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,23 @@ export class ServerUtils { .filter(Boolean) .map((part) => ApiUtils.safeEncodeURIComponent(part as string)), ); + + public static getErrorMessageFromResponse = async ( + res: Response, + ): Promise => { + let resBody: string | null; + let msg: string | null; + try { + resBody = await res?.text(); + } catch (err) { + resBody = null; + } + try { + msg = (await res.json()) as string; + } catch (err) { + msg = resBody; + } + + return msg; + }; } From 1540e934a172505cc2115494fdef63f524c8fdee Mon Sep 17 00:00:00 2001 From: Gimir Date: Thu, 1 Aug 2024 11:32:46 +0300 Subject: [PATCH 4/5] fix(chat): refactor error message parser Co-authored-by: Ilya Bondar --- apps/chat/src/utils/server/server.ts | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/apps/chat/src/utils/server/server.ts b/apps/chat/src/utils/server/server.ts index 94d3baed9b..d4422f45d0 100644 --- a/apps/chat/src/utils/server/server.ts +++ b/apps/chat/src/utils/server/server.ts @@ -22,19 +22,14 @@ export class ServerUtils { public static getErrorMessageFromResponse = async ( res: Response, ): Promise => { - let resBody: string | null; - let msg: string | null; - try { - resBody = await res?.text(); - } catch (err) { - resBody = null; + try { + return (await res.json()) as string; + } catch { + try { + return await res?.text(); + } catch { + return null; + } } - try { - msg = (await res.json()) as string; - } catch (err) { - msg = resBody; - } - - return msg; }; } From 8f29d2808825d3baa9b347c6501ce1e35284d3c6 Mon Sep 17 00:00:00 2001 From: Magomed-Elbi Dzhukalaev Date: Thu, 1 Aug 2024 13:36:59 +0300 Subject: [PATCH 5/5] fix(chat): run formatters --- apps/chat/src/utils/server/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/chat/src/utils/server/server.ts b/apps/chat/src/utils/server/server.ts index d4422f45d0..e48aee7ba8 100644 --- a/apps/chat/src/utils/server/server.ts +++ b/apps/chat/src/utils/server/server.ts @@ -22,7 +22,7 @@ export class ServerUtils { public static getErrorMessageFromResponse = async ( res: Response, ): Promise => { - try { + try { return (await res.json()) as string; } catch { try {