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 |
fromError(error: Error): AutonymError 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 |
getMessage(): string Gets the error message. |
|
public |
getPayload(): object 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 |
|
public |
Checks if the error is a client error and its code is one of |
|
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.
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:
Name | Type | Attribute | Description |
error | Error | An error object. |
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:
Name | Type | Attribute | Description |
code | string |
|
One of the error code static constants, or any other identifiable value for the error
type. If falsy, will fall back to |
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 |
|
data | object |
|
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 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
.
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
.
public isClientError(): boolean source
Checks if the error is a client error and its code is one of Autonym.CLIENT_ERRORS
.
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.
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