-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.ts
110 lines (93 loc) · 2.61 KB
/
error.ts
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @file Attribute Errors
*/
import type { AttrName } from './types.js';
/**
* Options that describe the error.
*
* @typedef {Object} ErrorInit
*
* @property {*} [cause] - A property indicating the specific cause of the error.
*/
interface ErrorInit
{
cause?: unknown;
}
declare type ErrorInterface = Error;
declare class Error implements ErrorInterface
{
name: string;
message: string;
stack?: string;
cause?: unknown;
constructor(message?: string, init?: ErrorInit);
/**
* {@link https://v8.dev/docs/stack-trace-api#stack-trace-collection-for-custom-exceptions}.
*/
// eslint-disable-next-line @typescript-eslint/ban-types -- Allow catch-all 'function' parameter type.
static captureStackTrace(error: object, constructor?: Function): void;
}
/**
* Represents an error with a value to be filtered.
*/
export class FilterError extends Error
{
/**
* @param {string} [message]
* @param {ErrorInit} [options]
*/
constructor(message?: string, options?: ErrorInit)
{
super(message, options);
Object.setPrototypeOf(this, new.target.prototype);
this.name = this.constructor.name;
// Maintains proper stack trace for where our error was thrown
// (only available on V8)
/* c8 ignore start */
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor);
} else {
this.stack = (new Error(message)).stack;
}
/* c8 ignore stop */
}
/**
* Creates an error with message where placeholder `{attr}`
* is substituded with name or value.
*
* @param {string} message
* @param {unknown} value
* @param {AttrName} [name]
* @param {ErrorInit} [options]
* @returns {FilterError}
*/
static create(message: string, value: unknown, name?: AttrName, options?: ErrorInit): FilterError
{
const attr = this.describeAttr(value, name);
return new this(
message.replace('{attr}', attr),
options
);
}
/**
* Describes the attribute name or value.
*
* @param {unknown} value
* @param {AttrName} [name]
* @returns {string}
*/
static describeAttr(value: unknown, name?: AttrName): string
{
if (name != null) {
return `attribute [${name}]`;
}
const type = (typeof value);
if (type !== 'object') {
return type;
}
if (value == null) {
return 'null';
}
return value.constructor.name;
}
}