Skip to content

Commit

Permalink
fix: app.server is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
linyyyang committed Nov 10, 2024
1 parent 0eb30d4 commit 3d43f4e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
7 changes: 4 additions & 3 deletions src/catcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Usage:
* @Author: richen
* @Date: 2022-02-21 11:32:03
* @LastEditTime: 2024-11-07 11:34:09
* @LastEditTime: 2024-11-10 23:36:27
*/

import { KoattyContext } from "koatty_core";
Expand Down Expand Up @@ -36,10 +36,11 @@ export function catcher<T extends Exception>(ctx: KoattyContext, err: Error | Ex
globalErrorHandler?: any, _ext?: extensionOptions) {
err.message = err.message || ctx.message || "";
if (err.message.includes('"')) {
err.message = err.message.replaceAll('"', '\\"');
// err.message = err.message.replaceAll('"', '\\"');
err.message = err.message.replace(/"/g, '\\"');
}
// 如果是异常对象,直接返回
if (isException(err)) {
if (isException(err) && span) {
return (<Exception>err).setSpan(span).handler(ctx);
}
// 执行自定义全局异常处理
Expand Down
49 changes: 30 additions & 19 deletions src/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
* @Author: richen
* @Date: 2020-11-20 17:37:32
* @LastEditors: Please set LastEditors
* @LastEditTime: 2024-11-07 11:33:58
* @LastEditTime: 2024-11-10 23:46:14
* @License: BSD (3-Clause)
* @Copyright (c) - <richenlin(at)gmail.com>
*/
import { v4 as uuidv4 } from "uuid";
import { Helper } from "koatty_lib";
import { IOCContainer } from "koatty_container";
import { Koatty, KoattyContext, KoattyNext } from "koatty_core";
import { FORMAT_HTTP_HEADERS, Span, Tracer } from "opentracing";
import { asyncLocalStorage, createAsyncResource, wrapEmitter } from './wrap';
import { httpHandler } from './handler/http';
import { Helper } from "koatty_lib";
import { FORMAT_HTTP_HEADERS, Tracer } from "opentracing";
import { v4 as uuidv4 } from "uuid";
import { extensionOptions } from "./catcher";
import { gRPCHandler } from './handler/grpc';
import { httpHandler } from './handler/http';
import { wsHandler } from './handler/ws';
import { extensionOptions } from "./catcher";
import { asyncLocalStorage, createAsyncResource, wrapEmitter } from './wrap';

/**
* TraceOptions
Expand Down Expand Up @@ -53,23 +53,26 @@ const defaultOptions = {
const respWapper = async (ctx: KoattyContext, next: KoattyNext,
options: TraceOptions, ext: extensionOptions) => {
// metadata
ctx.setMetaData(options.RequestIdName, ctx.requestId);
if (options.RequestIdName) ctx.setMetaData(options.RequestIdName, ctx.requestId);
// protocol handler
switch (ctx.protocol) {
case "grpc":
// allow bypassing koa
ctx.respond = false;
ctx.rpc.call.metadata.set(options.RequestIdName, ctx.requestId);
if (ctx.rpc && options.RequestIdName)
ctx.rpc.call.metadata.set(options.RequestIdName, ctx.requestId);
return gRPCHandler(ctx, next, ext);
case "ws":
case "wss":
// allow bypassing koa
ctx.respond = false;
ctx.set(options.RequestIdHeaderName, ctx.requestId);
if (options.RequestIdHeaderName)
ctx.set(options.RequestIdHeaderName, ctx.requestId);
return wsHandler(ctx, next, ext);
default:
// response header
ctx.set(options.RequestIdHeaderName, ctx.requestId);
if (options.RequestIdHeaderName)
ctx.set(options.RequestIdHeaderName, ctx.requestId);
return httpHandler(ctx, next, ext);
}
}
Expand Down Expand Up @@ -101,7 +104,7 @@ export function Trace(options: TraceOptions, app: Koatty) {

// server terminated
let terminated = false;
if (app.server.status === 503) {
if (app?.server?.status === 503) {
ctx.status = 503;
ctx.set('Connection', 'close');
ctx.body = 'Server is in the process of shutting down';
Expand All @@ -116,23 +119,31 @@ export function Trace(options: TraceOptions, app: Koatty) {
// http version
Helper.define(ctx, 'version', "2.0");
const request: any = ctx.getMetaData("_body")[0] || {};
requestId = `${ctx.getMetaData(options.RequestIdName)[0]}` ||
`${request[options.RequestIdName] || ''}`;
requestId = `${ctx.getMetaData(<string>options.RequestIdName)[0]}` ||
`${request[<string>options.RequestIdName] || ''}`;
break;
default:
// originalPath
Helper.define(ctx, 'originalPath', ctx.path);
// http version
Helper.define(ctx, 'version', ctx.req.httpVersion);
const requestIdHeaderName = options.RequestIdHeaderName.toLowerCase();
requestId = <string>ctx.headers[requestIdHeaderName] ||
`${ctx.query[options.RequestIdName] || ''}`;
if (options.RequestIdHeaderName) {
const requestIdHeaderName = options.RequestIdHeaderName.toLowerCase();
const headerRequestIdValue = request.headers[requestIdHeaderName];
if (Helper.isArray(headerRequestIdValue)) {
requestId = headerRequestIdValue.join(".");
} else {
requestId = headerRequestIdValue;
}
requestId = requestId ||
`${ctx.query[<string>options.RequestIdName] || ''}`;
}
break;
}

requestId = requestId || getTraceId(options);
Helper.define(ctx, 'requestId', requestId);
let span: Span;
let span;
// opten trace
if (options.OpenTrace) {
const serviceName = app.name || "unknownKoattyProject";
Expand Down Expand Up @@ -177,7 +188,7 @@ export function Trace(options: TraceOptions, app: Koatty) {
*/
function getTraceId(options?: TraceOptions) {
let rid;
if (Helper.isFunction(options.IdFactory)) {
if (Helper.isFunction(options?.IdFactory)) {
rid = options?.IdFactory();
}
return rid || uuidv4();
Expand Down

0 comments on commit 3d43f4e

Please sign in to comment.