koajs / json-error Goto Github PK
View Code? Open in Web Editor NEWError handler for pure-JSON apps
License: MIT License
Error handler for pure-JSON apps
License: MIT License
Hey, I use json-error recently and found it great. I really appreciate your work.
Is there any plan to add a feature of customize http code when ctx.thow
, just like:
app.use(koaJsonError({ status: 200 }))
I just want to set http code 200 and only put the error code into response body of my end point api.
Thanks, anyway.
Usually in the production environment application error should not display the error message, but redirect to the error page
When there is a: MongoError: E11000 duplicate I get this error. How can I handle this a bit better?
{
"message": "Not Found",
"name": "NotFoundError",
"stack": "NotFoundError: Not Found\n at Object.throw (/media/phil/Backup/sys/seoblog/api/node_modules/koa/lib/context.js:97:11)\n at /media/phil/Backup/sys/seoblog/api/node_modules/koa-json-error/lib/middleware.js:52:58\n at processTicksAndRejections (internal/process/task_queues.js:93:5)",
"status": 404
}
a serializing function option is probably the best bet
I use json-error
like the this:
...
koa.use(error({
preFormat: null,
format: (error) => {
const {status, message, code, detail} = error;
return {
status,
message,
code,
detail
}
}
}));
...
and it works as expected. However, I noticed recently that my error
event handler is now ignored:
koa.on('error', (error, ctx) => {
// this never fires on error anymore
if (!error.status || error.status === 500) {
logger.error(error.stack);
}
ctx.message = error.message;
logger.error(`!! [${ctx.state.id}] ${error.status}`, error.message);
});
If I remove json-error
middleware, the error
event handler starts to work again.
I guess I can move my handler to json-error
format/preFormat, but that should be documented.
From the readme it is not clear to me how to have an error that contains just status and message. I know from postFormat that I can omit fields from the response. But I don't want them to be created in the first place. I have to use options.format (Function)
to achieve that but how?
Some middlewares I use won't don't throw when something is wrong, but will return a 500.
Currently you handle only 404:
Line 52 in 9d5fe96
This is meant to simplify the current API and offer an alternative to the preFormat
, format
, postFormat
functions, which may be confusing some users.
In addition to allow passing an options
object, we may benefit from receiving a single function
instead which should behave as a shorthand for:
let opts = {
format: (err) => {
let obj = {};
// Modify `obj` in any way
return obj;
}
}
app.use(error(opts));
So, the above should be equivalent to:
function formatError(err) {
let obj = {};
// Modify `obj` in any way
return obj;
}
app.use(error(formatError));
Docs should also be updated.
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.