Skip to content

Commit

Permalink
fix: remove exception
Browse files Browse the repository at this point in the history
  • Loading branch information
linyyyang committed Sep 12, 2023
1 parent 90c5ea3 commit e19920d
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 230 deletions.
2 changes: 1 addition & 1 deletion src/handler/code.ts → src/code.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:53:19
* @LastEditTime: 2023-01-13 10:02:11
* @LastEditTime: 2023-09-12 10:46:38
*/

/**
Expand Down
92 changes: 0 additions & 92 deletions src/exception.ts

This file was deleted.

36 changes: 2 additions & 34 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: 2023-07-27 23:32:21
* @LastEditTime: 2023-09-12 10:50:17
*/
import * as Helper from "koatty_lib";
import { KoattyContext } from "koatty_core";
Expand All @@ -12,7 +12,7 @@ import { Exception, isPrevent, StatusCodeConvert } from "koatty_exception";
import { catcher } from '../catcher';
import { Span, Tags } from "opentracing";
import { StatusBuilder } from "@grpc/grpc-js";
import { GrpcStatusCodeMap, HttpStatusCodeMap } from "./code";
import { GrpcStatusCodeMap, HttpStatusCodeMap } from "../code";

/**
* gRPCHandler
Expand Down Expand Up @@ -87,35 +87,3 @@ export async function gRPCHandler(ctx: KoattyContext, next: Function, ext?: any)
clearTimeout(response.timeout);
}
}


/**
* gRPC Exception handler
*
* @export
* @param {KoattyContext} ctx
* @param {Exception} err
* @returns {*} {Promise<any>}
*/
export function gRPCExceptionHandler(ctx: any, err: Exception): Promise<any> {
try {
let errObj, code = err.code ?? 2;
// http status convert to grpc status
const status = err.status || ctx.status;
if (!err.code && HttpStatusCodeMap.has(status)) {
code = StatusCodeConvert(status);
}
const body = ctx.body || err.message || GrpcStatusCodeMap.get(code) || null;

if (body) {
errObj = new StatusBuilder().withCode(code).withDetails(body).build();
} else {
errObj = new StatusBuilder().withCode(code).build();
}
return ctx.rpc.callback(errObj, null);
} catch (error) {
Logger.Error(error);
ctx.rpc.callback(new StatusBuilder().withCode(2).build(), null);
return;
}
}
34 changes: 2 additions & 32 deletions src/handler/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
* @Usage:
* @Author: richen
* @Date: 2021-11-19 00:14:59
* @LastEditTime: 2023-07-27 23:30:42
* @LastEditTime: 2023-09-12 10:50:06
*/
import { Helper } from "koatty_lib";
import { catcher } from "../catcher";
import { KoattyContext } from "koatty_core";
import { DefaultLogger as Logger } from "koatty_logger";
import { Exception, isPrevent } from "koatty_exception";
import { Span, Tags } from "opentracing";
import { HttpStatusCode, HttpStatusCodeMap } from "./code";
import { HttpStatusCode, HttpStatusCodeMap } from "../code";

/**
* httpHandler
Expand Down Expand Up @@ -83,33 +83,3 @@ export async function httpHandler(ctx: KoattyContext, next: Function, ext?: any)
clearTimeout(response.timeout);
}
}

/**
* HTTP Exception handler
*
* @export
* @param {KoattyContext} ctx
* @param {Exception} err
* @returns {*}
*/
export function httpExceptionHandler(ctx: any, err: Exception) {
try {
ctx.status = ctx.status || 500;
if (HttpStatusCodeMap.has(err.status)) {
ctx.status = <HttpStatusCode>err.status;
}

let contentType = 'application/json';
if (ctx.encoding !== false) {
contentType = `${contentType}; charset=${ctx.encoding}`;
}
ctx.type = contentType;
const msg = err.message || ctx.message || "";
const body = `{"code":${err.code || 1},"message":"${msg}","data":${ctx.body ? JSON.stringify(ctx.body) : (ctx.body || null)}}`;
ctx.set("Content-Length", `${Buffer.byteLength(body)}`);
return ctx.res.end(body);
} catch (error) {
Logger.Error(error);
return null;
}
}
26 changes: 2 additions & 24 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: 2023-07-27 23:32:51
* @LastEditTime: 2023-09-12 10:50:14
*/
import { inspect } from "util";
import * as Helper from "koatty_lib";
Expand All @@ -12,7 +12,7 @@ import { DefaultLogger as Logger } from "koatty_logger";
import { Exception, isPrevent } from "koatty_exception";
import { catcher } from "../catcher";
import { Span, Tags } from "opentracing";
import { HttpStatusCode, HttpStatusCodeMap } from "./code";
import { HttpStatusCode, HttpStatusCodeMap } from "../code";

/**
* wsHandler
Expand Down Expand Up @@ -98,25 +98,3 @@ export async function wsHandler(ctx: KoattyContext, next: Function, ext?: any):

}

/**
* Websocket Exception handler
*
* @export
* @param {KoattyContext} ctx
* @param {Exception} err
* @returns {*} {void}
*/
export function wsExceptionHandler(ctx: any, err: Exception): void {
try {
ctx.status = ctx.status || 500;
if (HttpStatusCodeMap.has(err.status)) {
ctx.status = <HttpStatusCode>err.status;
}
const msg = err.message || ctx.message || "";
const body = `{"code":${err.code || 1},"message":"${msg}","data":${ctx.body ? JSON.stringify(ctx.body) : (ctx.body || null)}}`;
return ctx.websocket.send(body);
} catch (error) {
Logger.Error(error);
return null;
}
}
5 changes: 1 addition & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
* @Author: richen
* @Date: 2020-11-20 17:37:32
* @LastEditors: Please set LastEditors
* @LastEditTime: 2023-08-21 15:45:44
* @LastEditTime: 2023-09-12 10:54:06
* @License: BSD (3-Clause)
* @Copyright (c) - <richenlin(at)gmail.com>
*/

export * from "./trace";
export * from "./exception";
export * from "./utils";

43 changes: 0 additions & 43 deletions src/utils.ts

This file was deleted.

0 comments on commit e19920d

Please sign in to comment.