@basalt-lab/basalt-logger
    Preparing search index...

    Class BasaltError<T>

    A custom error class that extends the native Error class, providing additional properties such as a unique identifier, error key, HTTP status code, and cause.

    The following example demonstrates how to throw and catch a BasaltError.

    try {
    throw new BasaltError({
    message: 'An error occurred',
    key: 'example.error',
    httpStatusCode: 400,
    cause: new Error('Original error')
    });
    } catch (error) {
    if (error instanceof BasaltError) {
    console.error(`Error UUID: ${error.uuid}`);
    console.error(`Error Date: ${error.date}`);
    console.error(`Error Key: ${error.key}`);
    console.error(`HTTP Status Code: ${error.httpStatusCode}`);
    console.error(`Cause: ${error.cause}`);
    }
    }

    The following example demonstrates how to create a BasaltError with a custom cause type.

    const basaltError = new BasaltError<{ foo: string }>({
    message: 'Custom error with cause',
    key: 'basalt-package.error.custom_error',
    httpStatusCode: 500,
    cause: { foo: 'bar' },
    });
    console.log(basaltError.cause); // { foo: 'bar' }

    Type Parameters

    • const T = unknown

      The type of the cause of the error, which can be any object or error.

    Hierarchy

    • Error
      • BasaltError
    Index

    Constructors

    Properties

    cause: undefined | T

    The cause of the error, typically used to store the original error or additional context.

    message: string
    name: string
    stack?: string
    stackTraceLimit: number

    The maximum number of stack frames to capture.

    Accessors

    • get httpStatusCode(): number

      Gets the HTTP status code associated with the error.

      Returns number

      The HTTP status code.

    • get key(): string

      Gets the error key, which identifies the type of error.

      Returns string

      The key associated with the error.

    Methods

    • Create .stack property on a target object

      Parameters

      • targetObject: object
      • OptionalconstructorOpt: Function

      Returns void

    • Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

      const myObject = {};
      Error.captureStackTrace(myObject);
      myObject.stack; // Similar to `new Error().stack`

      The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

      The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

      The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

      function a() {
      b();
      }

      function b() {
      c();
      }

      function c() {
      // Create an error without stack trace to avoid calculating the stack trace twice.
      const { stackTraceLimit } = Error;
      Error.stackTraceLimit = 0;
      const error = new Error();
      Error.stackTraceLimit = stackTraceLimit;

      // Capture the stack trace above function b
      Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
      throw error;
      }

      a();

      Parameters

      • targetObject: object
      • OptionalconstructorOpt: Function

      Returns void

    • Check if a value is an instance of Error

      Parameters

      • value: unknown

        The value to check

      Returns value is Error

      True if the value is an instance of Error, false otherwise