Expressify exports some error types it uses internally.

const {
    AmbassifyError,
    ValidationError,
    InvalidOrganizationError,
    InsufficientPermissionsError,
} = require('@ambassify/expressify/error');

AmbassifyError

Base error class. This is an export of @ambassify/error. Extend this to get the most out of our custom serializers. If you install @ambassify/error yourself, you might end up with a different version which will break expressify’s serializers.

ValidationError

A validation error, contains at least the following properties:

  • message: description of the error
  • path: ordered array where each element is the accessor to the value where the error happened

The validation middleware for example will add extra properties returned by Joi.

InvalidOrganizationError

An authorization error indicating an invalid organization, contains at least the following properties:

  • message: description of the error
  • expected: the expected organization (can be a Boolean too)
  • actual: the organization found in the request

The authoirzation middleware uses this when the error handler is not customized.

InsufficientPermissionsError

An authorization error, contains at least the following properties:

  • message: description of the error
  • got: an array containing the owner scope of the request
  • need: an object { anyOf, allOf, noneOf } containing the parsed and interpolated required scope for the request

The authorization middleware uses this when the error handler is not customized.

BadRequestError

An error caused by an invalid request, contains at least the following properties:

  • message: description of the error

This error is used as a base class for body parsing errors.

Body parsing errors

A method (createBodyParserErrorClass) is provided that converts one of the error types thrown by body-parser into a BadRequestError error type.

Any error class produced by this method is a subclass of BadRequestError and it will have the camel-cased type as the class name and code.

If the expose property of the body-parser error is set to true the message will be passed on to the client. If it is set to false a generic message will be shown.

Internally this method is used to generate clean AmbassifyError instances for any body parsing error that might occur. At the time of writing that might be any of the following:

Error Name HTTP Status Code Original Message shown
EncodingUnsupportedError 415 Yes
EntityParseFailedError 400 Yes
EntityVerifyFailedError 403 Yes
RequestAbortedError 400 Yes
EntityTooLargeError 413 Yes
RequestSizeInvalidError 400 Yes
StreamEncodingSetError 500 No
StreamNotReadableError 500 No
ParametersTooManyError 413 Yes
CharsetUnsupportedError 415 Yes

Logging errors

An error logging utility method is also provided that helps you log errors using an Expressify-compatible logger. It takes care of detecting the right log level, logref, …

const { logError } = require('@ambassify/expressify/error');
const error = new Error('it broke');

logError(error, { logger: myLogger, req, res });