Home Reference Source
import AutonymError from 'autonym/lib/AutonymError'
public class | source

AutonymError

Wrapper for any error that occurred in a policy or store method.

Static Member Summary

Static Public Members
public static

Code indicating the server could not understand the request due to invalid syntax.

public static

The error codes that indicate client request errors, rather than internal errors.

public static

Code indicating the client does not have access rights to the content.

public static

Default.

public static

Code indicating the requested store method is not available for this model.

public static

Code indicating the requested resource does not exist.

public static

Code indicating the client must be authenticated to perform this action.

public static

Code indicating the request was improper, i.e.

Static Method Summary

Static Public Methods
public static

Wraps the given error and returns an instance of AutonymError, or returns the given error if it already is an AutonymError.

Constructor Summary

Public Constructor
public

constructor(code: string, message: string, data: object)

Method Summary

Public Methods
public

Gets the error code.

public

Gets the error metadata.

public

Gets the error message.

public

Gets the data to send in the HTTP response.

public

Gets the HTTP status code for the error based on its code, or falls back to AutonymError.INTERNAL_SERVER_ERROR.

public

Checks if the error is a client error and its code is one of Autonym.CLIENT_ERRORS.

public

Creates a copy of this error with a flag on it indicating it is a client error.

Static Public Members

public static BAD_REQUEST: string source

Code indicating the server could not understand the request due to invalid syntax.

public static CLIENT_ERRORS: string[] source

The error codes that indicate client request errors, rather than internal errors.

public static FORBIDDEN: string source

Code indicating the client does not have access rights to the content.

public static INTERNAL_SERVER_ERROR: string source

Default. Code indicating an unhandled error occurred while the server was processing the request.

public static METHOD_NOT_ALLOWED: string source

Code indicating the requested store method is not available for this model.

public static NOT_FOUND: string source

Code indicating the requested resource does not exist.

public static UNAUTHORIZED: string source

Code indicating the client must be authenticated to perform this action.

public static UNPROCESSABLE_ENTITY: string source

Code indicating the request was improper, i.e. failed schema validation.

Static Public Methods

public static fromError(error: Error): AutonymError source

Wraps the given error and returns an instance of AutonymError, or returns the given error if it already is an AutonymError. If the error object has the property code, it will be used as the AutonymError code. If the error object implements a toJSON method, its result will be stored as additional data.

Params:

NameTypeAttributeDescription
error Error

An error object.

Return:

AutonymError

The instance of AutonymError.

Example:

const err = new Error('Something bad happened')
const autonymError = AutonymError.fromError(err)
console.log(autonymError.getPayload()) // { message: 'An internal server error occurred.' }
const err = new Error('Something bad happened')
err.code = AutonymError.BAD_REQUEST
const autonymError = AutonymError.fromError(err)
// Still internal server error until we call `#toClientError()`
console.log(autonymError.getPayload()) // { message: 'An internal server error occurred.' }

Public Constructors

public constructor(code: string, message: string, data: object) source

Params:

NameTypeAttributeDescription
code string
  • optional

One of the error code static constants, or any other identifiable value for the error type. If falsy, will fall back to AutonymError.INTERNAL_SERVER_ERROR.

message string

A human-readable description of the error. It will only be passed to the client in a response if the code is one of Autonym.CLIENT_ERRORS.

data object
  • optional

Additional metadata to store on the error.

Example:

const autonymError = new AutonymError(null, 'Something bad happened')
const autonymError = new AutonymError(AutonymError.BAD_REQUEST, 'Something bad happened', { invalid: 'xyz' })

Public Methods

public getCode(): string source

Gets the error code.

Return:

string

The error code.

public getData(): object source

Gets the error metadata.

Return:

object

The error metadata.

public getMessage(): string source

Gets the error message.

Return:

string

The error message.

public getPayload(): object source

Gets the data to send in the HTTP response. It will only return the error data and message if the error has been converted to a client error and its code is one of Autonym.CLIENT_ERRORS.

Return:

object

The error payload. At a minimum, this object will have a message property.

Example:

const err = new AutonymError(AutonymError.INTERNAL_SERVER_ERROR, 'Something bad happened.', { x: 2 })
console.log(err.getPayload()) // { message: 'An internal server error occurred.' }
const err = new AutonymError(AutonymError.BAD_REQUEST, 'Something bad happened.', { x: 2 })
console.log(err.getPayload()) // { x: 2, message: 'Something bad happened.' }

public getStatus(): number source

Gets the HTTP status code for the error based on its code, or falls back to AutonymError.INTERNAL_SERVER_ERROR.

Return:

number

The HTTP status code.

public isClientError(): boolean source

Checks if the error is a client error and its code is one of Autonym.CLIENT_ERRORS.

Return:

boolean

Whether it is a client error.

public toClientError(): AutonymError source

Creates a copy of this error with a flag on it indicating it is a client error. Any error thrown will by default be an internal server error, until it is converted to a client error and it has an applicable error code.

Return:

AutonymError

The client error.

Example:

const clientError = (new AutonymError(null, 'Something bad happened')).toClientError()
console.log(clientError.isClientError()) // false
console.log(clientError.getPayload()) // { message: 'An internal server error occurred.' }
console.log(clientError.getStatus()) // 500
const clientError = (new AutonymError(AutonymError.BAD_REQUEST, 'Something bad happened')).toClientError()
console.log(clientError.isClientError()) // true
console.log(clientError.getPayload()) // { message: 'Something bad happened' }
console.log(clientError.getStatus()) // 400