@veloss/error
v0.0.5
Published
Handle errors in a simple and easily scalable way.
Downloads
8
Maintainers
Readme
@veloss/error
간단하고 쉽게 확장 가능한 방식으로 오류를 처리 할 수 있도록 도와주는 라이브러리입니다.
Usage
Install:
# npm
npm install @veloss/error
# yarn
yarn add @veloss/error
# pnpm
pnpm add @veloss/errorImport:
// ESM / Typescript
import { BaseError, HttpError, AuthError, ... } from "@veloss/error";
// CommonJS
const { BaseError, HttpError, AuthError, ... } = require("@veloss/error");BaseError
Base error class. This is used to create a new error class that can be extended
name
Type: string
Error name. This is used to identify the error class and should be unique among
const error = new BaseError("message");
error.name; // "BaseError"fatal
Type: boolean
Whether the error is fatal. This is used to determine whether the process should
const error = new BaseError("message", { fatal: true });
error.fatal; // trueunhandled
Type: boolean
Whether the error is unhandled. This is used to determine whether the error was
const error = new BaseError("message", { unhandled: true });
error.unhandled; // truedata
Type: DataT
Additional data. This can be any type and is used to store any additional
const error = new BaseError("message", { data: { key: "value" } });
error.data; // { key: "value" }cause
Type: unknown
The error that caused this error. This is used to store the error that caused
const error = new BaseError("message", { cause: new Error("cause") });
error.cause; // Error: causecode
Type: number
Error code. This is used to store an error code and is used to identify the
const error = new BaseError("message", { code: 404 });
error.code; // 404toJSON
Type: () => Pick<BaseError<DataT>, "message" | "data">
Converts the error to a JSON-serializable object. This is used to serialize the
const error = new BaseError("message", { data: { key: "value" } });
error.toJSON(); // { message: "message", data: { key: "value" } }staticMethods.base_error
Type: boolean
Whether the error is a BaseError. This is used to determine whether an error
BaseError.__base_error__; // trueBasic Usage
const error = new BaseError("message");createBaseError
CreateBaseError is a helper function that creates a new error class that extends BaseError. This is used to create a new error class that can be extended and is used to create a new error class that can be extended.
import { createBaseError } from "@veloss/error";
const error = createBaseError("CustomError");isBaseError
isBaseError is a helper function that checks whether an error is an instance of BaseError. This is used to determine whether an error is an instance of BaseError and is used to determine whether an error is an instance of BaseError.
import { isBaseError } from "@veloss/error";
const error = new BaseError("message");
isBaseError(error); // trueHttpError
HTTP error class. BaseError is extended to create a new error class that can be extended and is used to HTTP errors.
staticMethods.http_error
Type: boolean
Whether the error is a HttpError. This is used to determine whether an error
HttpError.__http_error__; // truestatusCode
Type: number
HTTP status code. This is used to store an HTTP status code and is used to
const error = new HttpError("message", { statusCode: 404 });
error.statusCode; // 404statusMessage
Type: string
HTTP status message. This is used to store an HTTP status message and is used to
const error = new HttpError("message", { statusMessage: "Not Found" });
error.statusMessage; // "Not Found"Basic Usage
const error = new HttpError("message", { statusCode: 404 });createHttpError
createHttpError is a helper function that creates a new error class that extends HttpError. This is used to create a new error class that can be extended and is used to create a new error class that can be extended.
import { createHttpError } from "@veloss/error";
const error = createHttpError("CustomHttpError", { statusCode: 404 });isHttpError
isHttpError is a helper function that checks whether an error is an instance of HttpError. This is used to determine whether an error is an instance of HttpError and is used to determine whether an error is an instance of HttpError.
import { isHttpError } from "@veloss/error";
const error = new HttpError("message", { statusCode: 404 });
isHttpError(error); // trueAuthError
Authentication error class. BaseError is extended to create a new error class that can be extended and is used to authentication errors.
staticMethods.auth_error
Type: boolean
Whether the error is a AuthError. This is used to determine whether an error
AuthError.__auth_error__; // trueerrorCode
Type: AuthErrorCode
Error code. This is used to store an error code and is used to identify the
const error = new AuthError("message", { errorCode: "unexpected_failure" });
error.errorCode; // "unexpected_failure"statusCode
Type: number
HTTP status code. This is used to store an HTTP status code and is used to
const error = new AuthError("message", { statusCode: 404 });
error.statusCode; // 404Basic Usage
const error = new AuthError("message", { errorCode: "unexpected_failure" });createAuthError
createAuthError is a helper function that creates a new error class that extends AuthError. This is used to create a new error class that can be extended and is used to create a new error class that can be extended.
import { createAuthError } from "@veloss/error";
const error = createAuthError("CustomAuthError", {
errorCode: "unexpected_failure",
});isAuthError
isAuthError is a helper function that checks whether an error is an instance of AuthError. This is used to determine whether an error is an instance of AuthError and is used to determine whether an error is an instance of AuthError.
import { isAuthError } from "@veloss/error";
const error = new AuthError("message", { errorCode: "unexpected_failure" });
isAuthError(error); // true