Err

Bajo error class, a thin wrapper around the node's Error object.

Every Bajo plugin has a built-in method called error which basically the shortcut to create a new Err instance. It helps you create this instance anywhere in your code quickly without the hassle of importing & instantiating:

// anywhere inside your code
if (notfound) throw this.error('Sorry, item is nowhere to be found!')

Constructor

new Err(plugin, msg, …argsopt)

Constructor.

Parameters:
NameTypeAttributesDescription
pluginPlugin

Plugin instance

msgstring

Error message

args*<optional>
<repeatable>

Variables to interpolate with error message. Error's payload can be pushed to the very last argument if needed

Members

message :string

Translated message

Type:
  • string

orgMessage :string

Original message before translation

Type:
  • string

payload :Object

Error payload extracted from the last arguments, if any

Type:
  • Object

Methods

fatal()

Print instance on console and terminate process.

formatErrorDetails(value) → {Object}

Pretty format error details. Used to format error payloads that contain details property, which is an array of error details.

Formatted error will be applied directly to the value parameter, and a detailsMessage will be returned for display purpose.

Parameters:
NameTypeDescription
valueObject

Value to format

Returns:
  • Formatted result and details message
Type: 
Object

write() → {Err}

Write message to the console. Chainable method, returns the Err instance itself.

If a payload is provided, it will be merged with the existing payload. If details is provided in the payload, it will be formatted and merged to the existing payload and a detailsMessage will be generated for display purpose.

Original message and translated message will be stored in orgMessage and message properties respectively.

ns property will be set to the plugin's namespace.

Returns:
  • Error object, useful for chaining
Type: 
Err

Type Definitions

TErrDetails

Error details object type definition. This type is used to represent detailed information about an error, including the field that caused the error, the error message, the value that caused the error, and any additional context.

This type is particularly useful for validation errors, where you want to provide detailed feedback about what went wrong.

Type:
  • Object
Properties
NameTypeAttributesDescription
fieldstring

Field name

errorstring

Error message

value*<optional>

The value that caused the error

extObject<optional>

Additional context about the error