• Creates new custom Error constructor. Use it to create errors to throw.

    Typeparam

    D - details object interface

    Example

    Create basic custom error

    const MyError = createError("MyError");
    throw new MyError("My custom problem happened");

    Example

    Create custom error with specified details shape

    const HttpError = createError<{ code: number, url: string }>("HttpError");
    throw new HttpError("Not Found", { code: 404, url: "/image.gif" });

    Example

    Create custom error defining parent error to inherit from

    const HttpError = createError<{ code: number, url: string }>("HttpError");
    const NotFoundError = createError({ url: string }, HttpError);

    const err = new NotFoundError("Not Found", { url: "/image.gif" });
    err instanceof NotFoundError; // true
    err instanceof HttpError; // true

    Returns

    Type Parameters

    • D

    Parameters

    • name: string

      error name

    • ParentError: ErrorConstructor = Error

      Error to inherit from (built-in or custom error)

    • Optional options: Options

      options to override global options

    Returns CustomErrorConstructor<D>

Generated using TypeDoc