fix(server): websocket error backward compatibility (#7346)

This commit is contained in:
forehalo
2024-06-26 12:22:33 +00:00
parent 6f9a4bb01a
commit bd9cb41f8a
3 changed files with 24 additions and 5 deletions

View File

@@ -76,7 +76,7 @@ export class UserFriendlyError extends Error {
this.data = args;
}
json() {
toJSON() {
return {
status: this.status,
code: STATUS_CODES[this.status] ?? 'BAD REQUEST',

View File

@@ -62,7 +62,7 @@ export type GraphqlContext = {
error.originalError instanceof UserFriendlyError
) {
// @ts-expect-error allow assign
formattedError.extensions = error.originalError.json();
formattedError.extensions = error.originalError.toJSON();
formattedError.extensions.stacktrace = error.originalError.stack;
return formattedError;
} else {

View File

@@ -38,12 +38,31 @@ export class GlobalExceptionFilter extends BaseExceptionFilter {
error.log('HTTP');
metrics.controllers.counter('error').add(1, { status: error.status });
const res = host.switchToHttp().getResponse<Response>();
res.status(error.status).send(error.json());
res.status(error.status).send(error.toJSON());
return;
}
}
}
/**
* Only exists for websocket error body backward compatibility
*
* relay on `code` field instead of `name`
*
* @TODO(@forehalo): remove
*/
function toWebsocketError(error: UserFriendlyError) {
// should be `error.toJSON()` after backward compatibility removed
return {
status: error.status,
code: error.name.toUpperCase(),
type: error.type.toUpperCase(),
name: error.name.toUpperCase(),
message: error.message,
data: error.data,
};
}
export const GatewayErrorWrapper = (event: string): MethodDecorator => {
// @ts-expect-error allow
return (
@@ -67,7 +86,7 @@ export const GatewayErrorWrapper = (event: string): MethodDecorator => {
.add(1, { event, status: mappedError.status });
return {
error: mappedError.json(),
error: toWebsocketError(mappedError),
};
}
};
@@ -82,6 +101,6 @@ export function mapSseError(originalError: any) {
metrics.sse.counter('error').add(1, { status: error.status });
return of({
type: 'error' as const,
data: error.json(),
data: error.toJSON(),
});
}