mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 12:55:00 +00:00
refactor(server): add debug info on global exception log (#10118)
before  after 
This commit is contained in:
@@ -136,7 +136,7 @@ export class UserFriendlyError extends Error {
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
log(context: string) {
|
||||
log(context: string, debugInfo?: object) {
|
||||
// ignore all user behavior error log
|
||||
if (
|
||||
this.type !== 'internal_server_error' &&
|
||||
@@ -148,11 +148,15 @@ export class UserFriendlyError extends Error {
|
||||
const logger = new Logger(context);
|
||||
const fn = this.status >= 500 ? logger.error : logger.log;
|
||||
|
||||
fn.call(
|
||||
logger,
|
||||
this.name,
|
||||
this.cause ? ((this.cause as any).stack ?? this.cause) : this.stack
|
||||
);
|
||||
let message = this.name;
|
||||
if (debugInfo) {
|
||||
message += ` (${JSON.stringify(debugInfo)})`;
|
||||
}
|
||||
let stack = this.stack ?? '';
|
||||
if (this.cause) {
|
||||
stack += `\n\nCaused by:\n\n${(this.cause as any).stack ?? this.cause}`;
|
||||
}
|
||||
fn.call(logger, message, stack);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
UserFriendlyError,
|
||||
} from '../error';
|
||||
import { metrics } from '../metrics';
|
||||
import { getRequestIdFromHost } from '../utils';
|
||||
|
||||
export function mapAnyError(error: any): UserFriendlyError {
|
||||
if (error instanceof UserFriendlyError) {
|
||||
@@ -45,7 +46,9 @@ export class GlobalExceptionFilter extends BaseExceptionFilter {
|
||||
// see '../graphql/logger-plugin.ts'
|
||||
throw error;
|
||||
} else {
|
||||
error.log('HTTP');
|
||||
error.log('HTTP', {
|
||||
requestId: error.requestId ?? getRequestIdFromHost(host),
|
||||
});
|
||||
metrics.controllers
|
||||
.counter('error')
|
||||
.add(1, { status: error.status, type: error.type, error: error.name });
|
||||
|
||||
@@ -84,16 +84,17 @@ export function parseCookies(
|
||||
* Request type
|
||||
*
|
||||
* @description
|
||||
* - `req`: http request
|
||||
* - `graphql`: graphql request
|
||||
* - `http`: http request
|
||||
* - `ws`: websocket request
|
||||
* - `se`: server event
|
||||
* - `job`: cron job
|
||||
* - `rpc`: rpc request
|
||||
*/
|
||||
export type RequestType = 'req' | 'ws' | 'se' | 'job' | 'rpc';
|
||||
export type RequestType = GqlContextType | 'se' | 'job';
|
||||
|
||||
export function genRequestId(type: RequestType) {
|
||||
return `${AFFiNE.flavor.type}:${type}-${randomUUID()}`;
|
||||
return `${AFFiNE.flavor.type}:${type}/${randomUUID()}`;
|
||||
}
|
||||
|
||||
export function getOrGenRequestId(type: RequestType) {
|
||||
@@ -101,3 +102,16 @@ export function getOrGenRequestId(type: RequestType) {
|
||||
// but it can be lost in unexpected scenarios, such as unit tests, where it is automatically generated.
|
||||
return ClsServiceManager.getClsService()?.getId() ?? genRequestId(type);
|
||||
}
|
||||
|
||||
export function getRequestIdFromRequest(req: Request, type: RequestType) {
|
||||
const traceContext = req.headers['x-cloud-trace-context'] as string;
|
||||
const traceId = traceContext ? traceContext.split('/', 1)[0] : undefined;
|
||||
if (traceId) return traceId;
|
||||
return genRequestId(type);
|
||||
}
|
||||
|
||||
export function getRequestIdFromHost(host: ArgumentsHost) {
|
||||
const type = host.getType<GqlContextType>();
|
||||
const req = getRequestFromHost(host);
|
||||
return getRequestIdFromRequest(req, type);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user