Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BaseError

BaseError

BaseError is base class for all other errors. It extends Error class with additional functionality.

Hierarchy

  • Error
    • BaseError

Index

Constructors

constructor

  • new BaseError(code: string, ...args: Array<Error | Object | string>): BaseError
  • Creates new error

    Parameters

    • code: string
    • Rest ...args: Array<Error | Object | string>

    Returns BaseError

Properties

code

code: string

Code of the error

example

user_not_found

context

context: any

Context assicated with an error

example

{username: 'someuser'}

defaultUserMessage

defaultUserMessage: string

Default message if user does not provide one

Optional errorMessage

errorMessage: undefined | string

Message that user passed into error

errors

errors: Array<Error> = []

Array of child errors passed to this error

id

id: string = Math.round(Math.pow(36, 48 + 1) - Math.random() * Math.pow(36, 48)).toString(36).slice(1)

Unique error id

example

n8lqujm20w4ckw40scss0o8coco0o004k0oc808o0osksooc'

message

message: string

Error message

example

[code=user_not_found] user was not found in database

name

name: string = this.constructor.name

Error name

default

this.constructor.name

example

NotFoundError

Static Error

Error: ErrorConstructor

Static message

message: string

Static name

name: string

Static Optional stack

stack: undefined | string

Accessors

userMessage

  • get userMessage(): string
  • Gets user formatted message.

    example

    class UserError extends BaseError { get userMessage() { if (this.context.userId) { return There was error with user ${this.context.userId}; }

    return super.userMessage;
    

    } }

    Returns string

Methods

_toKVString

  • _toKVString(obj: Object): string
  • Parameters

    • obj: Object

    Returns string

formatMessage

  • formatMessage(): string
  • Error message formatting method

    Returns string

toJSON

  • toJSON(): object
  • Converts error to JSON

    example

    error.toJSON();

    { id: "n8lqujm20w4ckw40scss0o8coco0o004k0oc808o0osksooc", code: "entity_not_found", message: "entity was not found in the database" context: { id: "1" } }

    Returns object

    • code: string
    • context: any
    • id: string
    • message: string

Static withContext

  • withContext<E>(this: E, context?: any): E
  • Defines error class with predefined context. This allows to reuse error context.

    Note: this is usually used by factory to define group of errors with some context.

    example

    const error = new BaseError.withContext({ userId: 'afece0d2-53a9-4610-b3de-0a8501cd3b91' });

    const error = new error('user_not_allowed');

    Type parameters

    Parameters

    • this: E
    • Default value context: any = {}

    Returns E

Generated using TypeDoc