diff --git a/package-lock.json b/package-lock.json index b901ad3..c0acc5d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "koatty_trace", - "version": "1.12.3", + "version": "1.12.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "koatty_trace", - "version": "1.12.3", + "version": "1.12.4", "license": "BSD-3-Clause", "dependencies": { "koatty_container": "^1.x.x", diff --git a/src/catcher.ts b/src/catcher.ts index 715a82f..b8af6fb 100644 --- a/src/catcher.ts +++ b/src/catcher.ts @@ -3,9 +3,10 @@ * @Usage: * @Author: richen * @Date: 2022-02-21 11:32:03 - * @LastEditTime: 2024-11-10 23:36:27 + * @LastEditTime: 2024-11-10 23:54:44 */ +import { IOCContainer } from "koatty_container"; import { KoattyContext } from "koatty_core"; import { Exception, isException } from "koatty_exception"; import { Helper } from "koatty_lib"; @@ -32,26 +33,29 @@ export interface extensionOptions { * @param _ext * @returns */ -export function catcher(ctx: KoattyContext, err: Error | Exception | T, span?: Span, - globalErrorHandler?: any, _ext?: extensionOptions) { - err.message = err.message || ctx.message || ""; - if (err.message.includes('"')) { - // err.message = err.message.replaceAll('"', '\\"'); - err.message = err.message.replace(/"/g, '\\"'); - } +export function catcher( + ctx: KoattyContext, + err: Error | Exception | T, + span?: Span, + globalErrorHandler?: any, + _ext?: extensionOptions +) { + const message = err.message || ctx.message || ""; + const sanitizedMessage = message.includes('"') ? message.replace(/"/g, '\\"') : message; + // 如果是异常对象,直接返回 if (isException(err) && span) { return (err).setSpan(span).handler(ctx); } // 执行自定义全局异常处理 - if (isConstructor(globalErrorHandler)) { - const ins: Exception = new globalErrorHandler(err.message, (err).code, (err).status, err.stack, span); - if (Helper.isFunction(ins.handler)) { - return ins.handler(ctx); - } + const ins: Exception = IOCContainer.getInsByClass(globalErrorHandler, + [err.message, (err).code, (err).status, err.stack, span]) + if (Helper.isFunction(ins?.handler)) { + return ins.handler(ctx); } + // 使用默认异常处理 - return new Exception(err.message, 1, 500, err.stack, span).handler(ctx); + return new Exception(sanitizedMessage, 1, 500, err.stack, span).handler(ctx); } /** diff --git a/src/handler/grpc.ts b/src/handler/grpc.ts index 9ffb3f6..fd49fcd 100644 --- a/src/handler/grpc.ts +++ b/src/handler/grpc.ts @@ -3,7 +3,7 @@ * @Usage: * @Author: richen * @Date: 2021-11-19 00:23:06 - * @LastEditTime: 2024-11-07 11:31:40 + * @LastEditTime: 2024-11-10 23:59:37 */ import { IRpcServerWriteableStream, KoattyContext } from "koatty_core"; import { Exception, StatusCodeConvert } from "koatty_exception"; @@ -22,10 +22,10 @@ export async function gRPCHandler(ctx: KoattyContext, next: Function, ext?: exte // Encoding ctx.encoding = ext.encoding; - ctx.rpc.call.metadata.set('X-Powered-By', 'Koatty'); - ctx.rpc.call.sendMetadata(ctx.rpc.call.metadata); + ctx?.rpc?.call?.metadata?.set('X-Powered-By', 'Koatty'); + ctx?.rpc?.call?.sendMetadata(ctx.rpc.call.metadata); - const span = ext.span; + const span = ext?.span; if (span) { span.setTag(Tags.HTTP_URL, ctx.originalUrl); span.setTag(Tags.HTTP_METHOD, ctx.method); @@ -49,7 +49,7 @@ export async function gRPCHandler(ctx: KoattyContext, next: Function, ext?: exte // ctx = null; }; ctx.res.once("finish", finish); - (>ctx.rpc.call).once("error", finish); + (>ctx?.rpc?.call).once("error", finish); // try /catch const response: any = {}; diff --git a/src/handler/http.ts b/src/handler/http.ts index 3dec7cf..e8b3261 100644 --- a/src/handler/http.ts +++ b/src/handler/http.ts @@ -3,14 +3,14 @@ * @Usage: * @Author: richen * @Date: 2021-11-19 00:14:59 - * @LastEditTime: 2024-11-07 11:32:58 + * @LastEditTime: 2024-11-11 00:01:09 */ -import { Stream } from 'stream'; -import { catcher, extensionOptions } from "../catcher"; import { KoattyContext } from "koatty_core"; -import { DefaultLogger as Logger } from "koatty_logger"; import { Exception } from "koatty_exception"; +import { DefaultLogger as Logger } from "koatty_logger"; import { Span, Tags } from "opentracing"; +import { Stream } from 'stream'; +import { catcher, extensionOptions } from "../catcher"; // StatusEmpty const StatusEmpty = [204, 205, 304]; @@ -37,7 +37,7 @@ export async function httpHandler(ctx: KoattyContext, next: Function, ext?: exte } // response finish - ctx.res.once('finish', () => { + ctx?.res?.once('finish', () => { const now = Date.now(); const msg = `{"action":"${ctx.method}","status":"${ctx.status}","startTime":"${ctx.startTime}","duration":"${(now - ctx.startTime) || 0}","requestId":"${ctx.requestId}","endTime":"${now}","path":"${ctx.originalPath || '/'}"}`; Logger[(ctx.status >= 400 ? 'Error' : 'Info')](msg); diff --git a/src/handler/ws.ts b/src/handler/ws.ts index d83c226..e98e14c 100644 --- a/src/handler/ws.ts +++ b/src/handler/ws.ts @@ -3,7 +3,7 @@ * @Usage: * @Author: richen * @Date: 2021-11-19 00:24:43 - * @LastEditTime: 2024-02-01 10:03:17 + * @LastEditTime: 2024-11-11 00:01:48 */ import { KoattyContext } from "koatty_core"; import { Exception } from "koatty_exception"; @@ -48,7 +48,7 @@ export async function wsHandler(ctx: KoattyContext, next: Function, ext?: extens } // ctx = null; } - ctx.res.once("finish", finish); + ctx?.res?.once("finish", finish); // ctx.websocket.once("error", finish); // ctx.websocket.once("connection", () => {