Files
wwjcloud/src/common/exception/exception.interface.ts

353 lines
6.6 KiB
TypeScript
Raw Normal View History

/**
*
* NestJS
* 参考: https://docs.nestjs.cn/fundamentals/exception-filters
* Java: 异常抽象
*/
export interface ExceptionInterface {
/**
*
* @param exception
* @param context
* @returns
*/
handle(exception: any, context?: any): Promise<ExceptionResult>;
/**
*
* @param exception
* @param context
*/
log(exception: any, context?: any): Promise<void>;
/**
*
* @param exception
* @param context
* @returns
*/
format(exception: any, context?: any): ExceptionResponse;
/**
*
* @param exception
* @returns
*/
getType(exception: any): ExceptionType;
/**
*
* @param exception
* @returns
*/
getSeverity(exception: any): ExceptionSeverity;
/**
*
* @param exception
* @returns
*/
shouldLog(exception: any): boolean;
/**
*
* @param exception
* @returns
*/
shouldReport(exception: any): boolean;
}
/**
*
*/
export interface ExceptionResult {
success: boolean;
response: ExceptionResponse;
logged: boolean;
reported: boolean;
handled: boolean;
}
/**
*
*/
export interface ExceptionResponse {
statusCode: number;
message: string;
error: string;
timestamp: string;
path?: string;
method?: string;
traceId?: string;
spanId?: string;
correlationId?: string;
details?: any;
code?: string;
type?: ExceptionType;
severity?: ExceptionSeverity;
}
/**
*
*/
export enum ExceptionType {
VALIDATION = 'validation',
AUTHENTICATION = 'authentication',
AUTHORIZATION = 'authorization',
NOT_FOUND = 'not_found',
CONFLICT = 'conflict',
BAD_REQUEST = 'bad_request',
INTERNAL_SERVER_ERROR = 'internal_server_error',
SERVICE_UNAVAILABLE = 'service_unavailable',
TIMEOUT = 'timeout',
RATE_LIMIT = 'rate_limit',
BUSINESS = 'business',
SYSTEM = 'system',
NETWORK = 'network',
DATABASE = 'database',
CACHE = 'cache',
EXTERNAL_API = 'external_api',
UNKNOWN = 'unknown',
}
/**
*
*/
export enum ExceptionSeverity {
LOW = 'low',
MEDIUM = 'medium',
HIGH = 'high',
CRITICAL = 'critical',
}
/**
*
*/
export interface ExceptionContext {
request?: {
method: string;
url: string;
headers: Record<string, string>;
body?: any;
query?: Record<string, any>;
params?: Record<string, any>;
ip?: string;
userAgent?: string;
userId?: string;
sessionId?: string;
};
response?: {
statusCode: number;
headers: Record<string, string>;
body?: any;
};
user?: {
id: string;
username?: string;
email?: string;
role?: string;
permissions?: string[];
};
environment?: {
nodeEnv: string;
version: string;
hostname: string;
pid: number;
};
trace?: {
traceId: string;
spanId: string;
correlationId: string;
};
meta?: Record<string, any>;
}
/**
*
*/
export interface ExceptionConfig {
enabled: boolean;
logLevel: 'debug' | 'info' | 'warn' | 'error' | 'fatal';
reportLevel: 'error' | 'fatal';
includeStackTrace: boolean;
includeRequest: boolean;
includeResponse: boolean;
includeUser: boolean;
includeEnvironment: boolean;
sanitizeData: boolean;
maxMessageLength: number;
maxStackTraceLength: number;
rateLimit: {
enabled: boolean;
maxRequests: number;
windowMs: number;
};
reporting: {
enabled: boolean;
providers: string[];
filters: string[];
};
}
/**
*
*/
export interface ExceptionOptions {
/**
*
*/
type?: ExceptionType;
/**
*
*/
severity?: ExceptionSeverity;
/**
*
*/
log?: boolean;
/**
*
*/
report?: boolean;
/**
*
*/
message?: string;
/**
*
*/
statusCode?: number;
/**
*
*/
code?: string;
/**
*
*/
includeStackTrace?: boolean;
/**
*
*/
includeRequest?: boolean;
/**
*
*/
includeResponse?: boolean;
/**
*
*/
includeUser?: boolean;
/**
*
*/
includeEnvironment?: boolean;
/**
*
*/
sanitizeData?: boolean;
/**
*
*/
meta?: Record<string, any>;
}
/**
*
*/
export interface ExceptionFilterInterface {
/**
*
* @param exception
* @returns
*/
shouldHandle(exception: any): boolean;
/**
*
* @param exception
* @param context
* @returns
*/
handle(exception: any, context?: any): Promise<ExceptionResult>;
}
/**
*
*/
export interface ExceptionReporterInterface {
/**
*
* @param exception
* @param context
* @returns
*/
report(exception: any, context?: any): Promise<boolean>;
/**
*
* @param exceptions
* @param context
* @returns
*/
reportBatch(
exceptions: Array<{ exception: any; context?: any }>,
): Promise<boolean[]>;
/**
*
* @param exception
* @returns
*/
shouldReport(exception: any): boolean;
}
/**
*
*/
export interface ExceptionStatsInterface {
/**
*
* @param exception
* @param context
*/
record(exception: any, context?: any): void;
/**
*
* @param timeRange
* @returns
*/
getStats(timeRange?: { start: Date; end: Date }): ExceptionStats;
/**
*
*/
reset(): void;
}
/**
*
*/
export interface ExceptionStats {
total: number;
byType: Record<ExceptionType, number>;
bySeverity: Record<ExceptionSeverity, number>;
byTime: Array<{ time: Date; count: number }>;
topExceptions: Array<{ message: string; count: number; lastOccurred: Date }>;
rate: number; // 异常率
trend: 'increasing' | 'decreasing' | 'stable';
}