-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathException.ts
More file actions
executable file
·68 lines (60 loc) · 1.82 KB
/
Exception.ts
File metadata and controls
executable file
·68 lines (60 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { isObject, isString } from "../lang";
/**
* Base exception class.
*
* @extends Error
* @since 1.0.0
* @version 1.0.0
*/
export class Exception extends Error {
/**
* @param {unknown} [message] - The error message, an existing Error object, or any other value.
*/
constructor(message?: unknown) {
super(message instanceof Error ? message.message : isString(message) ? message : undefined);
this.name = new.target.name;
Object.setPrototypeOf(this, new.target.prototype);
}
/**
* Provides a custom string tag for `Object.prototype.toString.call()`.
*
* @returns {string} The name of the constructor.
*/
get [Symbol.toStringTag](): string {
return this.constructor.name;
}
/**
* Creates a new Exception instance.
*
* @param {unknown} [message] - The error message or an existing Error object.
* @returns {Exception} A new instance of the Exception class.
*/
static create(message?: unknown): Exception {
return new Exception(message);
}
/**
* Type guard to check if a given value is an instance of {@link Exception} or any of its subclasses.
*
* @param {unknown} value - The value to check.
* @returns {boolean} `true` if the value is an instance of `Exception` or a class that extends it; otherwise, `false`.
*/
static isException(value: unknown): value is Exception {
return isObject(value) && value instanceof Exception;
}
/**
* Retrieves the error message of the exception.
*
* @returns {string} The message string associated with the exception.
*/
getMessage(): string {
return this.message;
}
/**
* Returns a string representation of the exception.
*
* @returns {string} A string representing the exception.
*/
toString(): string {
return String(this.message ?? this.constructor.name);
}
}