mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-06 08:50:50 +08:00
fix(server): websocket error backward compatibility (#7346)
This commit is contained in:
@@ -76,7 +76,7 @@ export class UserFriendlyError extends Error {
|
|||||||
this.data = args;
|
this.data = args;
|
||||||
}
|
}
|
||||||
|
|
||||||
json() {
|
toJSON() {
|
||||||
return {
|
return {
|
||||||
status: this.status,
|
status: this.status,
|
||||||
code: STATUS_CODES[this.status] ?? 'BAD REQUEST',
|
code: STATUS_CODES[this.status] ?? 'BAD REQUEST',
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ export type GraphqlContext = {
|
|||||||
error.originalError instanceof UserFriendlyError
|
error.originalError instanceof UserFriendlyError
|
||||||
) {
|
) {
|
||||||
// @ts-expect-error allow assign
|
// @ts-expect-error allow assign
|
||||||
formattedError.extensions = error.originalError.json();
|
formattedError.extensions = error.originalError.toJSON();
|
||||||
formattedError.extensions.stacktrace = error.originalError.stack;
|
formattedError.extensions.stacktrace = error.originalError.stack;
|
||||||
return formattedError;
|
return formattedError;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -38,12 +38,31 @@ export class GlobalExceptionFilter extends BaseExceptionFilter {
|
|||||||
error.log('HTTP');
|
error.log('HTTP');
|
||||||
metrics.controllers.counter('error').add(1, { status: error.status });
|
metrics.controllers.counter('error').add(1, { status: error.status });
|
||||||
const res = host.switchToHttp().getResponse<Response>();
|
const res = host.switchToHttp().getResponse<Response>();
|
||||||
res.status(error.status).send(error.json());
|
res.status(error.status).send(error.toJSON());
|
||||||
return;
|
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 => {
|
export const GatewayErrorWrapper = (event: string): MethodDecorator => {
|
||||||
// @ts-expect-error allow
|
// @ts-expect-error allow
|
||||||
return (
|
return (
|
||||||
@@ -67,7 +86,7 @@ export const GatewayErrorWrapper = (event: string): MethodDecorator => {
|
|||||||
.add(1, { event, status: mappedError.status });
|
.add(1, { event, status: mappedError.status });
|
||||||
|
|
||||||
return {
|
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 });
|
metrics.sse.counter('error').add(1, { status: error.status });
|
||||||
return of({
|
return of({
|
||||||
type: 'error' as const,
|
type: 'error' as const,
|
||||||
data: error.json(),
|
data: error.toJSON(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user