A very simple collection of statuses suitable to be used with the express-errorlog package.
- Install and use
- Statuses
- Getting Statuses
- Overriding Messages
- Wrapping Errors
- Multiple overrides
- License (MIT)
Install as usual with NPM:
npm install --save express-statuses
Then use it in your express app!
var S = require('express-statuses');
...
app.get('/foobar', function(req, res, next) {
next(S.BAD_REQUEST);
})
The application error handler will see an object containing the following:
app.use(function(err, req, res, next) {
// err will be { status: 400, message: 'Bad Request' }
...
})
A handy collection of status codes can also be accessed via the C
sub-object:
app.post('/collection', function(req, res, next) {
...
res.sendStatus(S.C.CREATED);
})
Statuses can be referenced by name:
S.NOT_FOUND;
Or can be accessed by status code:
S(404);
All returned statuses can be passed to next(...)
as-is or (being functions)
can be used to further customize what is passed to the error handler.
Customization and inclusion can be done in few ways:
Messages can be overridden by simply invoking a status as a function:
next(S.NOT_FOUND('Something is amiss'));
will result in the error handler receiving the following as its first parameter:
{
"status": 404,
"message": "Something is amiss"
}
Errors can be nested for further logging by the error handler:
try {
...
} catch (error) {
next(S.BAD_REQUEST(error));
}
will result in the error handler receiving the following as its first parameter:
{
"status": 400,
"message": "Bad Request"
"error": ... // reference to the error for logging
}
For better express-errorlog integration, a number of parameters can be specified as options:
next(S.UNAUTHORIZED({
message: 'No way I am letting you in',
details: { key: 'Some extra stuff here' }
error: myCaughtException
}));
will result in the error handler receiving the following as its first parameter:
{
"status": 400,
"message": "No way I am letting you in",
"details": { "key": "Some extra stuff here" }
"error": // reference to myCaughtException
}
Listed here are all currently configured statuses:
- Informational
CONTINUE
: 100SWITCHING_PROTOCOLS
: 101PROCESSING
: 102
- Successful
OK
: 200CREATED
: 201ACCEPTED
: 202NON_AUTHORITATIVE_INFORMATION
: 203NO_CONTENT
: 204RESET_CONTENT
: 205PARTIAL_CONTENT
: 206MULTI_STATUS
: 207ALREADY_REPORTED
: 208IM_USED
: 226
- Redirection
MULTIPLE_CHOICES
: 300MOVED_PERMANENTLY
: 301FOUND
: 302SEE_OTHER
: 303NOT_MODIFIED
: 304USE_PROXY
: 305_UNUSED_
: 306TEMPORARY_REDIRECT
: 307PERMANENT_REDIRECT
: 308
- Client Errors
BAD_REQUEST
: 400UNAUTHORIZED
: 401PAYMENT_REQUIRED
: 402FORBIDDEN
: 403NOT_FOUND
: 404METHOD_NOT_ALLOWED
: 405NOT_ACCEPTABLE
: 406PROXY_AUTHENTICATION_REQUIRED
: 407REQUEST_TIMEOUT
: 408CONFLICT
: 409GONE
: 410LENGTH_REQUIRED
: 411PRECONDITION_FAILED
: 412PAYLOAD_TOO_LARGE
: 413URI_TOO_LONG
: 414UNSUPPORTED_MEDIA_TYPE
: 415RANGE_NOT_SATISFIABLE
: 416EXPECTATION_FAILED
: 417I_AM_A_TEAPOT
: 418UNPROCESSABLE_ENTITY
: 422LOCKED
: 423FAILED_DEPENDENCY
: 424UNORDERED_COLLECTION
: 425UPGRADE_REQUIRED
: 426PRECONDITION_REQUIRED
: 428TOO_MANY_REQUESTS
: 429REQUEST_HEADER_FIELDS_TOO_LARGE
: 431UNAVAILABLE_FOR_LEGAL_REASONS
: 451
- Server Errors
INTERNAL_SERVER_ERROR
: 500NOT_IMPLEMENTED
: 501BAD_GATEWAY
: 502SERVICE_UNAVAILABLE
: 503GATEWAY_TIMEOUT
: 504HTTP_VERSION_NOT_SUPPORTED
: 505VARIANT_ALSO_NEGOTIATES
: 506INSUFFICIENT_STORAGE
: 507LOOP_DETECTED
: 508BANDWIDTH_LIMIT_EXCEEDED
: 509NOT_EXTENDED
: 510NETWORK_AUTHENTICATION_REQUIRED
: 511
Copyright (c) 2015 USRZ.com and Pier Paolo Fumagalli
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.