-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
14 changed files
with
328 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var util = require('util'); | ||
|
||
/** | ||
* This implements an HTTP error code that should eventually be passed through | ||
* next() to an error handling middleware. | ||
* | ||
* @param {String} description - a custom description to be sent to the client | ||
* | ||
* @example | ||
* // import the error into a controller | ||
* const BadRequest = require('lib/errors/BadRequest'); | ||
* | ||
* // use the error in either a promise chain or directly via next() | ||
* return next(new BadRequest('Some description...')); | ||
* | ||
* @constructor | ||
*/ | ||
function BadRequest(description) { | ||
'use strict'; | ||
|
||
// make sure we have a working stack trace | ||
Error.captureStackTrace(this, this.constructor); | ||
|
||
// HTTP status code | ||
this.status = 400; | ||
|
||
// bhima status code (for $translation) | ||
this.code = 'ERRORS.BAD_REQUEST'; | ||
|
||
// default to an empty string if no description passed in | ||
this.description = description || ''; | ||
} | ||
|
||
// prototypically inherit from the global error object | ||
util.inherits(BadRequest, Error); | ||
|
||
// expose the function to an external controller | ||
module.exports = BadRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var util = require('util'); | ||
|
||
/** | ||
* This implements an HTTP error code that should eventually be passed through | ||
* next() to an error handling middleware. | ||
* | ||
* @param {String} description - a custom description to be sent to the client | ||
* | ||
* @example | ||
* // import the error into a controller | ||
* const Forbidden = require('lib/errors/Forbidden'); | ||
* | ||
* // use the error in either a promise chain or directly via next() | ||
* return next(new Forbidden('Some description...')); | ||
* | ||
* @constructor | ||
*/ | ||
function Forbidden(description) { | ||
'use strict'; | ||
|
||
// make sure we have a working stack trace | ||
Error.captureStackTrace(this, this.constructor); | ||
|
||
// HTTP status code | ||
this.status = 403; | ||
|
||
// bhima status code (for $translation) | ||
this.code = 'ERRORS.FORBIDDEN'; | ||
|
||
// default to an empty string if no description passed in | ||
this.description = description || ''; | ||
} | ||
|
||
// prototypically inherit from the global error object | ||
util.inherits(Forbidden, Error); | ||
|
||
// expose the function to an external controller | ||
module.exports = Forbidden; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var util = require('util'); | ||
|
||
/** | ||
* This implements an HTTP error code that should eventually be passed through | ||
* next() to an error handling middleware. | ||
* | ||
* @param {String} description - a custom description to be sent to the client | ||
* | ||
* @example | ||
* // import the error into a controller | ||
* const InternalServerError = require('lib/errors/InternalServerError'); | ||
* | ||
* // use the error in either a promise chain or directly via next() | ||
* return next(new InternalServerError('Some description...')); | ||
* | ||
* @constructor | ||
*/ | ||
function InternalServerError(description) { | ||
'use strict'; | ||
|
||
// make sure we have a working stack trace | ||
Error.captureStackTrace(this, this.constructor); | ||
|
||
// HTTP status code | ||
this.status = 500; | ||
|
||
// bhima status code (for $translation) | ||
this.code = 'ERRORS.INTERNAL_SERVER_ERROR'; | ||
|
||
// default to an empty string if no description passed in | ||
this.description = description || ''; | ||
} | ||
|
||
// prototypically inherit from the global error object | ||
util.inherits(InternalServerError, Error); | ||
|
||
// expose the function to an external controller | ||
module.exports = InternalServerError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var util = require('util'); | ||
|
||
/** | ||
* This implements an HTTP error code that should eventually be passed through | ||
* next() to an error handling middleware. | ||
* | ||
* @param {String} description - a custom description to be sent to the client | ||
* | ||
* @example | ||
* // import the error into a controller | ||
* const NotFound = require('lib/errors/NotFound'); | ||
* | ||
* // use the error in either a promise chain or directly via next() | ||
* return next(new NotFound('Some description...')); | ||
* | ||
* @constructor | ||
*/ | ||
function NotFound(description) { | ||
'use strict'; | ||
|
||
// make sure we have a working stack trace | ||
Error.captureStackTrace(this, this.constructor); | ||
|
||
// HTTP status code | ||
this.status = 404; | ||
|
||
// bhima status code (for $translation) | ||
this.code = 'ERRORS.NOT_FOUND'; | ||
|
||
// default to an empty string if no description passed in | ||
this.description = description || ''; | ||
} | ||
|
||
// prototypically inherit from the global error object | ||
util.inherits(NotFound, Error); | ||
|
||
// expose the function to an external controller | ||
module.exports = NotFound; |
Oops, something went wrong.