From 3f8d7d211533ea02c612979168c8f048a3d61c45 Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Tue, 10 Jan 2023 08:36:26 +0000 Subject: [PATCH] Made `SoftError` for recoverable errors --- src/utils/errors/soft-error.ts | 40 +++++++++++++++ tests/unit/utils/errors.ts/soft-error.spec.ts | 49 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/utils/errors/soft-error.ts create mode 100644 tests/unit/utils/errors.ts/soft-error.spec.ts diff --git a/src/utils/errors/soft-error.ts b/src/utils/errors/soft-error.ts new file mode 100644 index 0000000..6466ed0 --- /dev/null +++ b/src/utils/errors/soft-error.ts @@ -0,0 +1,40 @@ +/** + * Represents a soft error, indicating whether the error is recoverable or not. + */ +export class SoftError extends Error { + /** + * Indicates whether the error is recoverable or not. + */ + private readonly _isSoft: boolean; + + /** + * Initializes a new instance of the {@link SoftError} class. + * + * @param isSoft - Indicates whether the error is recoverable or not. + * @param message - An optional error message. + */ + constructor(isSoft: boolean, message?: string) { + super(message); + + this.name = "SoftError"; + this._isSoft = isSoft; + } + + /** + * Indicates whether the error is recoverable or not. + */ + get isSoft(): boolean { + return this._isSoft; + } +} + +/** + * Determines whether the specified error is a soft error. + * + * @param error - The error to check. + * + * @returns `true` if the error is soft (i.e., recoverable); otherwise, `false`. + */ +export function isSoftError(error: unknown): boolean { + return !!(error as SoftError)?.isSoft; +} diff --git a/tests/unit/utils/errors.ts/soft-error.spec.ts b/tests/unit/utils/errors.ts/soft-error.spec.ts new file mode 100644 index 0000000..ceb5c7e --- /dev/null +++ b/tests/unit/utils/errors.ts/soft-error.spec.ts @@ -0,0 +1,49 @@ +import { SoftError, isSoftError } from "@/utils/errors/soft-error"; + +describe("SoftError", () => { + describe("constructor", () => { + test("initializes with isSoft set to false", () => { + const error = new SoftError(false, "An error occurred."); + + expect(error).toBeInstanceOf(SoftError); + expect(error.name).toBe("SoftError"); + expect(error.message).toBe("An error occurred."); + expect(error.isSoft).toBe(false); + }); + + test("initializes with isSoft set to true", () => { + const error = new SoftError(true, "An error occurred."); + + expect(error).toBeInstanceOf(SoftError); + expect(error.name).toBe("SoftError"); + expect(error.message).toBe("An error occurred."); + expect(error.isSoft).toBe(true); + }); + }); +}); + +describe("isSoftError", () => { + test("returns true for SoftError with isSoft set to true", () => { + const error = new SoftError(true, "An error occurred."); + + expect(isSoftError(error)).toBe(true); + }); + + test("returns false for SoftError with isSoft set to false", () => { + const error = new SoftError(false, "An error occurred."); + + expect(isSoftError(error)).toBe(false); + }); + + test("returns false for non-SoftError errors", () => { + const error = new Error("An error occurred."); + + expect(isSoftError(error)).toBe(false); + }); + + test("returns false for non-error values", () => { + expect(isSoftError("string")).toBe(false); + expect(isSoftError(123)).toBe(false); + expect(isSoftError({ key: "value" })).toBe(false); + }); +});