mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-22 19:53:48 +08:00
feat(server): add requestId to all error response (#9790)
This commit is contained in:
@@ -2,6 +2,7 @@ import { STATUS_CODES } from 'node:http';
|
|||||||
|
|
||||||
import { HttpStatus, Logger } from '@nestjs/common';
|
import { HttpStatus, Logger } from '@nestjs/common';
|
||||||
import { capitalize } from 'lodash-es';
|
import { capitalize } from 'lodash-es';
|
||||||
|
import { ClsServiceManager } from 'nestjs-cls';
|
||||||
|
|
||||||
export type UserFriendlyErrorBaseType =
|
export type UserFriendlyErrorBaseType =
|
||||||
| 'bad_request'
|
| 'bad_request'
|
||||||
@@ -72,6 +73,11 @@ export class UserFriendlyError extends Error {
|
|||||||
*/
|
*/
|
||||||
data: any;
|
data: any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request id for tracing
|
||||||
|
*/
|
||||||
|
requestId?: string;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
type: UserFriendlyErrorBaseType,
|
type: UserFriendlyErrorBaseType,
|
||||||
name: keyof typeof USER_FRIENDLY_ERRORS,
|
name: keyof typeof USER_FRIENDLY_ERRORS,
|
||||||
@@ -93,6 +99,7 @@ export class UserFriendlyError extends Error {
|
|||||||
this.type = type;
|
this.type = type;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.data = args;
|
this.data = args;
|
||||||
|
this.requestId = ClsServiceManager.getClsService()?.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
@@ -103,6 +110,8 @@ export class UserFriendlyError extends Error {
|
|||||||
name: this.name.toUpperCase(),
|
name: this.name.toUpperCase(),
|
||||||
message: this.message,
|
message: this.message,
|
||||||
data: this.data,
|
data: this.data,
|
||||||
|
// only include requestId for server error
|
||||||
|
requestId: this.status >= 500 ? this.requestId : undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,6 +123,7 @@ export class UserFriendlyError extends Error {
|
|||||||
`Name: ${json.name}`,
|
`Name: ${json.name}`,
|
||||||
`Message: ${json.message}`,
|
`Message: ${json.message}`,
|
||||||
`Data: ${JSON.stringify(json.data)}`,
|
`Data: ${JSON.stringify(json.data)}`,
|
||||||
|
`RequestId: ${json.requestId}`,
|
||||||
].join('\n');
|
].join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ function toWebsocketError(error: UserFriendlyError) {
|
|||||||
name: error.name.toUpperCase(),
|
name: error.name.toUpperCase(),
|
||||||
message: error.message,
|
message: error.message,
|
||||||
data: error.data,
|
data: error.data,
|
||||||
|
requestId: error.requestId,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user