Skip to content

Commit

Permalink
fix: undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
linyyyang committed Nov 10, 2024
1 parent 73669bd commit 872684e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 18 additions & 14 deletions src/catcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -32,26 +33,29 @@ export interface extensionOptions {
* @param _ext
* @returns
*/
export function catcher<T extends Exception>(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<T extends Exception>(
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 (<Exception>err).setSpan(span).handler(ctx);
}
// 执行自定义全局异常处理
if (isConstructor(globalErrorHandler)) {
const ins: Exception = new globalErrorHandler(err.message, (<T>err).code, (<T>err).status, err.stack, span);
if (Helper.isFunction(ins.handler)) {
return ins.handler(ctx);
}
const ins: Exception = IOCContainer.getInsByClass(globalErrorHandler,
[err.message, (<T>err).code, (<T>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);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/handler/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 = <Span>ext.span;
const span = <Span>ext?.span;
if (span) {
span.setTag(Tags.HTTP_URL, ctx.originalUrl);
span.setTag(Tags.HTTP_METHOD, ctx.method);
Expand All @@ -49,7 +49,7 @@ export async function gRPCHandler(ctx: KoattyContext, next: Function, ext?: exte
// ctx = null;
};
ctx.res.once("finish", finish);
(<IRpcServerWriteableStream<any, any>>ctx.rpc.call).once("error", finish);
(<IRpcServerWriteableStream<any, any>>ctx?.rpc?.call).once("error", finish);

// try /catch
const response: any = {};
Expand Down
10 changes: 5 additions & 5 deletions src/handler/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/handler/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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", () => {
Expand Down

0 comments on commit 872684e

Please sign in to comment.