1.0.0 • Published 9 years ago

eror v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
9 years ago

eror

NPM version Build status Coveralls status Support us

Error utilities which provide error classes for HTTP response (4xx, 5xx) and more.

Installation

$ npm install eror

Example

// Formatted message
new eror.InvalidTypeError('"%s" must be a string!', 'name');

// Custom status code
new eror.MissingValueError(400, '"%s" is required!', 'age');

// Pre-defined HTTP errors
new eror.NotFoundError('User "%s" not found!', 'paul');

// Custom errors
eror(503, 'The Earth is %s!', 'exploding');

API reference

eror(statusCode, message || format, ...values)

Create a custom error.

eror(401, 'You are not logged in!');
eror('Welcome, %s %s.', 'John', 'Lennon');
  • statusCode: Status code as a number, defaults to 500.
  • message: Error message.
  • format: Placeholder string, please refer to util.format for more details.
  • ...values: Values to be placed in placeholder string.

new eror[name](statusCode, message || format, ...values)

Instantiate a new common error.

new eror.MissingValueError(400, '"%s" field is required!', 'name');
  • name: Class name of the error, as specified in common error list.
  • statusCode: Status code, if is not specified, default status code, 500, will be used.
  • message, format and ...values: as in eror().

new eror[name || statusCode](message || format, ...values)

Instantiate a new HTTP error.

new eror.NotFoundError('Requested resource not found!');
new eror[500]('Server exploded at %s!', new Date());
  • name: Class name of the error, as specified in HTTP error list.
  • statusCode: Status code of HTTP error.
  • message, format and ...values: as in eror().

HTTP error's status code cannot be overridden.

Error properties

var err = eror(404, 'Nothing!');

err.message; // Nothing!
err.statusCode; // 404
err.status; // also 404
  • message
  • statusCode and status: Status code of the error.

Error class properties

eror.InternalServerError.MESSAGE; // Internal Server Error
eror.InternalServerError.STATUS_CODE; // 500
eror.InternalServerError.STATUS; // also 500
  • MESSAGE: Default error message of errors which are instantiate from this class.
  • STATUS_CODE and STATUS: Default status code.

Error list

Common errors

Class nameStatus code
UnsupportedOperationError500
ReversedNameError500
NameAlreadyDefinedError500
InvalidOperationError500
InvalidTypeError500
MissingValueError500
InvalidValueError500

HTTP errors

Class nameStatus code
BadRequestError400
UnauthorizedError401
PaymentRequiredError402
ForbiddenError403
NotFoundError404
MethodNotAllowedError405
NotAcceptableError406
ProxyAuthenticationRequiredError407
RequestTimeoutError408
ConflictError409
GoneError410
LengthRequiredError411
PreconditionFailedError412
PayloadTooLargeError413
UriTooLongError414
UnsupportedMediaTypeError415
RangeNotSatisfiableError416
ExpectationFailedError417
ImATeapotError418
UnprocessableEntityError422
LockedError423
FailedDependencyError424
UnorderedCollectionError425
UpgradeRequiredError426
PreconditionRequiredError428
TooManyRequestsError429
RequestHeaderFieldsTooLargeError431
UnableForLegalReasonsError451
InternalServerError500
NotImplementedError501
BadGatewayError502
ServiceUnavailableError503
GatewayTimeoutError504
HttpVersionNotSupportedError505
VariantAlsoNegotiatesError506
InsufficientStorageError507
LoopDetectedError508
BandwidthLimitExceededError509
NotExtendedError510
NetworkAuthenticationRequiredError511

Contributing

  • Submit new issue if there is any common error, which is useful for you or others, unavailable.

  • In lieu of a formal styleguide, take care to maintain the existing coding style. .editorconfig and .jshintrc files can give you more details.

  • Add tests for any new or changed functionality/element.

  • Lint and test your code using npm test.

Author

License

Released under MIT license.