Skip to content

Commit

Permalink
fix(chat): add error messages for failed publish requests (Issue #1481)…
Browse files Browse the repository at this point in the history
… (#1840)

Co-authored-by: Magomed-Elbi Dzhukalaev <[email protected]>
Co-authored-by: Ilya Bondar <[email protected]>
  • Loading branch information
3 people authored Aug 1, 2024
1 parent 75fd22c commit 066fc55
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 26 deletions.
7 changes: 2 additions & 5 deletions apps/chat/src/pages/api/publication/approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions apps/chat/src/pages/api/publication/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions apps/chat/src/pages/api/publication/rulesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand Down
22 changes: 13 additions & 9 deletions apps/chat/src/store/publication/publication.epics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}),
);
}),
Expand All @@ -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),
),
),
);

Expand Down Expand Up @@ -813,7 +815,7 @@ const approvePublicationEpic: AppEpic = (action$, state$) =>
}),
catchError((err) => {
console.error(err);
return of(PublicationActions.approvePublicationFail());
return of(PublicationActions.approvePublicationFail(err.message));
}),
),
),
Expand All @@ -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),
),
),
);
Expand Down Expand Up @@ -907,7 +909,7 @@ const uploadRulesEpic: AppEpic = (action$) =>
}),
catchError((err) => {
console.error(err);
return of(PublicationActions.uploadRulesFail());
return of(PublicationActions.uploadRulesFail(err.message));
}),
),
),
Expand All @@ -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),
),
),
);

Expand Down
9 changes: 7 additions & 2 deletions apps/chat/src/utils/server/api.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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());
Expand Down
16 changes: 16 additions & 0 deletions apps/chat/src/utils/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -16,4 +18,18 @@ export class ServerUtils {
.filter(Boolean)
.map((part) => ApiUtils.safeEncodeURIComponent(part as string)),
);

public static getErrorMessageFromResponse = async (
res: Response,
): Promise<string | null> => {
try {
return (await res.json()) as string;
} catch {
try {
return await res?.text();
} catch {
return null;
}
}
};
}

0 comments on commit 066fc55

Please sign in to comment.