noamokman / express-json-error-handler Goto Github PK
View Code? Open in Web Editor NEWError handler for express JSON APIs
License: MIT License
Error handler for express JSON APIs
License: MIT License
Hey, can you push a new release that drops that in-production thingie?
Hello,
The handler expects err
argument to be of type Error, while in theory you can throw anything in JS - string, number, undefined etc. Universal error handler should expect type of err
: unknown
.
If the package is still maintained I could work on PR that checks for type of err
we get and if it's not Error
, construct an actual Error instance before handling it further.
If the package is still maintained, I could work on PR to address this. What do you think?
The way I do it in my current JSON error handling middleware is by using these functions:
function isErrorWithMessage(error: unknown): error is ErrorWithMessage {
return (
typeof error === "object" &&
error !== null &&
"message" in error &&
typeof (error as Record<string, unknown>).message === "string"
);
}
function toErrorWithMessage(maybeError: unknown): ErrorWithMessage {
if (isErrorWithMessage(maybeError)) return maybeError;
try {
return new Error(JSON.stringify(maybeError));
} catch {
/**
* fallback in case there's an error stringifying the maybeError like with
* circular references or bigint values for example.
*/
return new Error(String(maybeError));
}
}
Giving error for below code.
Node version: 20.11.0
import express from "express";
import jsonErrorHandler from "express-json-error-handler";
const app = express();
app.use(jsonErrorHandler());
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.