Comments (5)
First thing first, http errors are all about proper http code, so we should start designing error handler by http error code specification and errors thrown from controller, should include desired http error code (and error message) @adiferd
from expr.
for now, i use restify error event handler to handle exception in controller (i use restify for day to day work). Restify error handler wrapped in ErrorMap class. Node.js utilize next() method to move process to next chain, so the wrapper method called within node.js next method
return next(ErrorMap.mapping(error.status || 500, error.message || 'Internal server error'));
See this error map method snippet for reference
'use strict'
const errs = require('restify-errors');
class ErrorMap {
static mapping (code, message = null) {
const errorList = {
400: 'BadRequestError',
401: 'UnauthorizedError',
403: 'ForbiddenError',
404: 'NotFoundError',
405: 'MethodNotAllowedError',
408: 'RequestTimeoutError',
413: 'RequestEntityTooLargeError',
500: 'InternalServerError'
};
return new errs[errorList[code]](message);
}
}
module.exports= ErrorMap;
Since we are building this thing on top of express, we can't use restify's error handler event. Unfortunately, things going quite different in express for no error events are available, per this docs provided by express.
https://expressjs.com/en/guide/error-handling.html
So no other way than build error handler method by ourself @adiferd
from expr.
the next method called within exception in try catch
block, which is the catch
scope
from expr.
I think, I can start with refactor this function
'use strict'
class Formatter {
static toSingleResponse(data, message, error) {
return {
result: data
, message: message
, error: (error!=null)?error:null
};
}
remove the error
key from the return and build the ErrorMapper Function
toSingleResponse
will be used only to return success reponse, on the other hand errorMapper
will be used to return error message on API request. what do you think @firyalff ?
from expr.
this is good, since we're gonna move error response formater to separated module, +1 for this @adiferd
from expr.
Related Issues (5)
- Should add validator layer HOT 1
- error escape HOT 1
- uncaught exception handler HOT 1
- Change entrypoint script in config directory HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from expr.