mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-25 09:51:01 -05:00
Made SoftError
for recoverable errors
This commit is contained in:
parent
3fe2a3fdc9
commit
3f8d7d2115
2 changed files with 89 additions and 0 deletions
40
src/utils/errors/soft-error.ts
Normal file
40
src/utils/errors/soft-error.ts
Normal file
|
@ -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;
|
||||
}
|
49
tests/unit/utils/errors.ts/soft-error.spec.ts
Normal file
49
tests/unit/utils/errors.ts/soft-error.spec.ts
Normal file
|
@ -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);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue