-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Refactor Server Error Handling #182
Comments
I've noticed this during a number of code reviews - I think this is a very important change and should be included as soon as possible so that it can be applied to everyone's code. I will be referencing this issue and it can be scheduled to be tackled ASAP. |
Sounds good. Should I work on a PR with the errors pre-made, then developers can rebase and begin using them? |
Yes - this should be tackled before it becomes a larger problem; the problem described in this pull request is shown here I suggest a pull request demonstrating the structure of this proposal is made today - once reviewed it can be merged as soon as possible and future reviews will be able to reference that code as a good template to follow. Cheers. |
This commit creates error functions for common HTTP error status codes. The following have been implemented: 1. BadRequest [400] 2. Unauthorized [401] 3. Forbidden [403] 4. NotFound [404] 5. InternalServerError [500] Error functions are really easy to import and use in controllers. See the following example: ```js const NotFound = require('lib/errors/NotFound'); // throw the error to demonstrate that it is an error try { throw new NotFound('An example description.'); // NOTE - this used to be done like this: // throw new req.codes.ERR_NOT_FOUND(); // pass the error to the first error handling middleware } catch (e) { next(e); } ``` **NOTE** - this commit __deprecates__ the use of `req.codes`. All controller code should now import the codes they need and no longer depend the on `req.codes` property, as it will be removed in the future. Closes #182.
This commit creates error functions for common HTTP error status codes. The following have been implemented: 1. BadRequest [400] 2. Unauthorized [401] 3. Forbidden [403] 4. NotFound [404] 5. InternalServerError [500] Error functions are really easy to import and use in controllers. See the following example: ```js const NotFound = require('lib/errors/NotFound'); // throw the error to demonstrate that it is an error try { throw new NotFound('An example description.'); // NOTE - this used to be done like this: // throw new req.codes.ERR_NOT_FOUND(); // pass the error to the first error handling middleware } catch (e) { next(e); } ``` **NOTE** - this commit __deprecates__ the use of `req.codes`. All controller code should now import the codes they need and no longer depend the on `req.codes` property, as it will be removed in the future. Closes #182.
…y-reference feat(ui-grid): add reference sorting algorithm
The current state of error handling on the server is pretty poor. Here are some problems with it:
req
object on every request, even if they are never used. This has performance implications and makesreq.codes
seem like a magic object.next(new req.codes.ERR_SOME_ERROR())
).stack
property doesn't work, making it impossible to use it to trace errors.I propose the following refactor. Errors will be defined in files found in
server/lib/errors/*.js
. We will implement a subset of the error HTTP status codes in a few files. Specifically, this subset:BadRequest
(400)Unauthorized
(401)Forbidden
(403)NotFound
(404)InternalServerError
(500)These would appear in files named like the above, for example
/server/lib/errors/NotFound.js
. These files would contain a single function, exported viamodule.exports
. See below for a sample:A developer can then selectively import the errors they need and use them as follows:
This is obviously much cleaner to write and read. We avoid most of the problems pointed out at the top of this issue, and actually get clean stack traces!
Error inheritance inspired by this gist.
The text was updated successfully, but these errors were encountered: