Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(chat): add error messages for failed publish requests (Issue #1481) #1840

Merged
merged 13 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
IlyaBondar marked this conversation as resolved.
Show resolved Hide resolved
switchMap((errorMessage) =>
throwError(() => new Error(errorMessage)),
),
catchError(() => throwError(() => new Error(response.status + ''))),
);
}

return from(response.json());
Expand Down
21 changes: 21 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,23 @@ export class ServerUtils {
.filter(Boolean)
.map((part) => ApiUtils.safeEncodeURIComponent(part as string)),
);

public static getErrorMessageFromResponse = async (
res: Response,
): Promise<string | null> => {
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;
Gimir marked this conversation as resolved.
Show resolved Hide resolved
};
}