@luolapeikko/ts-common
    Preparing search index...

    Class ErrorCore

    Error core functions

    v1.0.0

    Index

    Methods

    • Wraps an error in a custom error type.

      Type Parameters

      • ET extends Error

        The custom error type that extends Error.

      Parameters

      • err: unknown

        The error to wrap.

      • ErrorClass: new (message?: string) => ET

        The constructor of the error class to use.

      • Optionalmessage: string | ((errMsg: string) => string)

        The optional override message.

      Returns ET

      An instance of the custom error type with the original message and stack trace.

      try {
      // ...
      } catch (err) {
      throw ErrorCore.as(err, TypeError); // throws TypeError
      throw ErrorCore.as(err, TypeError, 'custom message'); // throws TypeError with custom message
      throw ErrorCore.as(err, TypeError, (errMsg) => `custom message: ${errMsg}`); // throws TypeError with custom message prefix
      }

      v1.0.0

    • Asserts that the given value is an Error.

      Parameters

      • err: unknown

        Error to assert.

      Returns asserts err is Error

      If the given value is not an instance of the Error class.

      v1.0.0

      try {
      // ...
      } catch (err) {
      ErrCore.assert(err);
      console.log(err.message);
      }
    • Clones an error.

      Type Parameters

      • ET extends Error

      Parameters

      • source: ET

        The error to clone.

      • OptionalErrorClass: new (message?: string) => ET

        Optional custom error class to use for the cloned error.

      Returns ET

      A new error instance with the same message and properties as the source error.

      v1.0.5

    • Get or create an error from the given value, if it's not an error, it will be wrapped in an UnknownError.

      Parameters

      • err: unknown

        Error instance or error message.

      Returns Error | UnknownError

      Error instance.

      v1.0.0

      try {
      // ...
      } catch (err) {
      console.log(ErrCore.from(err).message);
      }
    • Checks if the given value is an instance of the Error class.

      Parameters

      • err: unknown

        Value to check.

      Returns err is Error

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

      v1.0.0

    • Wraps an error in a custom error type using a callback function.

      Type Parameters

      • ET extends Error

        The custom error type that extends Error.

      Parameters

      • err: unknown

        The original error or error message to wrap.

      • callback: (message?: string) => ET

        A function that takes an optional message and returns an instance of the custom error type.

      Returns ET

      An instance of the custom error type with the original message and stack trace.

      try {
      // ...
      } catch (err) {
      const customError = ErrorCore.with(err, (msg) => new CustomError(msg));
      throw customError;
      }

      v1.0.0