Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koatty_exception

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koatty_exception - npm Package Compare versions

Comparing version 1.3.1 to 1.3.2-0

0

.rollup.config.js

@@ -0,0 +0,0 @@ /*

9

CHANGELOG.md

@@ -5,8 +5,15 @@ # Changelog

### [1.3.1](https://github.com/koatty/koatty_exception/compare/v1.3.0...v1.3.1) (2024-01-21)
### [1.3.2-0](https://github.com/koatty/koatty_exception/compare/v1.3.1...v1.3.2-0) (2024-01-22)
### [1.3.1](https://github.com/koatty/koatty_exception/compare/v1.2.8...v1.3.1) (2024-01-21)
### Bug Fixes
* add set ([9b49044](https://github.com/koatty/koatty_exception/commit/9b4904409b0c2a8c90ff3662b5bfa83ad4b3b403))
* comment ([284b029](https://github.com/koatty/koatty_exception/commit/284b029409807205964244c3a30b54969bb348a0))
* default value ([d7a620a](https://github.com/koatty/koatty_exception/commit/d7a620a65d3ff301c2097c1630a40877d30eaa5e))
* exception method ([d330e03](https://github.com/koatty/koatty_exception/commit/d330e0373b95087ce0fbcb0ad609e83c79754a7e))
* span.finish ([0e65c54](https://github.com/koatty/koatty_exception/commit/0e65c54820ca1c37f468b830f03698026e75c540))
* 支持链式调用 ([5efdfe0](https://github.com/koatty/koatty_exception/commit/5efdfe07d16d9474f100f3b9465a342ee7a69abc))

@@ -13,0 +20,0 @@ ## [1.3.0](https://github.com/koatty/koatty_exception/compare/v1.3.0-3...v1.3.0) (2024-01-21)

13

dist/index.d.ts
/*!
* @Author: richen
* @Date: 2024-01-21 13:44:09
* @Date: 2024-01-22 10:20:01
* @License: BSD (3-Clause)

@@ -39,5 +39,12 @@ * @Copyright (c) - <richenlin(at)gmail.com>

/**
* Default exception handler
* @param ctx
* @description: logger
* @param {KoattyContext} ctx
* @return {*}
*/
log(ctx: KoattyContext): void;
/**
* @description: Default exception handler
* @param {KoattyContext} ctx
* @return {*}
*/
handler(ctx: KoattyContext): Promise<any>;

@@ -44,0 +51,0 @@ }

/*!
* @Author: richen
* @Date: 2024-01-21 13:43:58
* @Date: 2024-01-22 10:19:50
* @License: BSD (3-Clause)

@@ -12,5 +12,5 @@ * @Copyright (c) - <richenlin(at)gmail.com>

var koatty_logger = require('koatty_logger');
var grpcJs = require('@grpc/grpc-js');
var opentracing = require('opentracing');
var Helper = require('koatty_lib');
var grpcJs = require('@grpc/grpc-js');

@@ -524,110 +524,3 @@ function _interopNamespaceDefault(e) {

/*
* @Description:
* @Usage:
* @Author: richen
* @Date: 2022-02-10 17:39:54
* @LastEditTime: 2024-01-21 12:05:58
*/
/**
* gRPC error handler
*
* @export
* @param {KoattyContext} ctx
* @param {Exception} err
* @returns {*} {Promise<any>}
*/
function gRPCHandler(ctx, err) {
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 grpcJs.StatusBuilder().withCode(code).withDetails(body).build();
}
else {
errObj = new grpcJs.StatusBuilder().withCode(code).build();
}
ctx.rpc.callback(errObj, null);
}
catch (error) {
koatty_logger.DefaultLogger.Error(error);
ctx.rpc.callback(new grpcJs.StatusBuilder().withCode(2).build(), null);
}
return null;
}
/*
* @Description:
* @Usage:
* @Author: richen
* @Date: 2022-02-19 15:42:47
* @LastEditTime: 2023-09-12 10:58:42
*/
/**
* HTTP error handler
*
* @export
* @param {KoattyContext} ctx
* @param {Exception} err
* @returns {*}
*/
function httpHandler(ctx, err) {
try {
ctx.status = ctx.status || 500;
if (HttpStatusCodeMap.has(err.status)) {
ctx.status = 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) {
koatty_logger.DefaultLogger.Error(error);
return null;
}
}
/*
* @Description:
* @Usage:
* @Author: richen
* @Date: 2022-02-10 18:16:36
* @LastEditTime: 2024-01-21 12:05:44
*/
/**
* Websocket error handler
*
* @export
* @param {KoattyContext} ctx
* @param {Exception} err
* @returns {*} {void}
*/
function wsHandler(ctx, err) {
try {
ctx.status = ctx.status || 500;
if (HttpStatusCodeMap.has(err.status)) {
ctx.status = 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) {
koatty_logger.DefaultLogger.Error(error);
return null;
}
}
/**
* @ author: richen

@@ -696,8 +589,15 @@ * @ copyright: Copyright (c) - <richenlin(at)gmail.com>

/**
* Default exception handler
* @param ctx
* @description: logger
* @param {KoattyContext} ctx
* @return {*}
*/
handler(ctx) {
log(ctx) {
const now = Date.now();
let message = `{"startTime":"${ctx.startTime}","duration":"${(now - ctx.startTime) || 0}","requestId":"${ctx.requestId}","endTime":"${now}","path":"${ctx.originalPath || '/'}","message":"${this.message}"`;
// LOG
this.stack ? koatty_logger.DefaultLogger.Error(this.stack) : koatty_logger.DefaultLogger.Error(this.message);
if (this.stack) {
message = `${message},stack:"${this.stack}"`;
}
message = `${message}}`;
koatty_logger.DefaultLogger.Error(message);
// span

@@ -710,13 +610,52 @@ if (this.span) {

this.span.log({ 'event': 'error', 'message': this.message, 'stack': this.stack });
// this.span.finish();
}
switch (ctx.protocol) {
case "grpc":
return gRPCHandler(ctx, this);
case "ws":
case "wss":
return wsHandler(ctx, this);
default:
return httpHandler(ctx, this);
return;
}
/**
* @description: Default exception handler
* @param {KoattyContext} ctx
* @return {*}
*/
handler(ctx) {
// LOG
this.log(ctx);
try {
ctx.status = this.status || ctx.status;
let contentType = 'application/json';
if (ctx.encoding !== false) {
contentType = `${contentType}; charset=${ctx.encoding}`;
}
ctx.type = contentType;
let body = JSON.stringify(ctx.body || "");
switch (ctx.protocol) {
case "ws":
case "wss":
if (ctx.websocket) {
body = `{"code": ${this.code}, "message": "${this.message}", "data": ${body}}`;
ctx.length = Buffer.byteLength(body);
ctx.websocket.send(body);
}
break;
case "grpc":
if (ctx.rpc && ctx.rpc.callback) {
// http status convert to grpc status
if (!this.code) {
this.code = StatusCodeConvert(ctx.status);
}
body = body || GrpcStatusCodeMap.get(this.code) || "";
ctx.length = Buffer.byteLength(body);
ctx.rpc.callback(new grpcJs.StatusBuilder().withCode(this.code).withDetails(body).build(), null);
}
break;
default:
body = `{"code": ${this.code}, "message": "${this.message}", "data": ${body}}`;
ctx.length = Buffer.byteLength(body);
ctx.res.end(body);
break;
}
}
catch (error) {
koatty_logger.DefaultLogger.Error(error);
}
return null;
}

@@ -723,0 +662,0 @@ }

{
"name": "koatty_exception",
"version": "1.3.1",
"version": "1.3.2-0",
"description": "Exception handler for koatty.",

@@ -5,0 +5,0 @@ "scripts": {

# koatty_exception
Exception handler for Koatty.
{
"name": "koatty_exception",
"version": "1.3.1",
"version": "1.3.2-0",
"description": "Exception handler for koatty.",

@@ -5,0 +5,0 @@ "scripts": {

# koatty_exception
Exception handler for Koatty.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc