-
Notifications
You must be signed in to change notification settings - Fork 28
/
errors.ts
104 lines (90 loc) · 2.86 KB
/
errors.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
/* tslint:disable:max-classes-per-file */
interface ErrorConstructor {
new (...args: any[]): Error;
}
/**
* Workaround for custom errors when compiling typescript targeting 'ES5'.
* see: https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
* @param {CustomError} error
* @param newTarget the value of `new.target`
* @param {Function} errorType
*/
function fixError(
error: Error,
newTarget: ErrorConstructor,
errorType: ErrorConstructor
) {
Object.setPrototypeOf(error, errorType.prototype);
// when an error constructor is invoked with the `new` operator
if (newTarget === errorType) {
error.name = newTarget.name;
// exclude the constructor call of the error type from the stack trace.
if (Error.captureStackTrace) {
Error.captureStackTrace(error, errorType);
} else {
const stack = new Error(error.message).stack;
if (stack) {
error.stack = fixStack(stack, `new ${newTarget.name}`);
}
}
}
}
function fixStack(stack: string, functionName: string) {
if (!stack) return stack;
if (!functionName) return stack;
// exclude lines starts with: " at functionName "
const exclusion: RegExp = new RegExp(`\\s+at\\s${functionName}\\s`);
const lines = stack.split("\n");
const resultLines = lines.filter((line) => !line.match(exclusion));
return resultLines.join("\n");
}
/// CUSTOM ERRORS ///
// When no WebLN provider is available
export class MissingProviderError extends Error {
constructor(message: string) {
super(message);
fixError(this, new.target, MissingProviderError);
}
}
// When the user rejects a request
export class RejectionError extends Error {
constructor(message: string) {
super(message);
fixError(this, new.target, RejectionError);
}
}
// When the node can't be connected to (i.e. the app did nothing wrong)
export class ConnectionError extends Error {
constructor(message: string) {
super(message);
fixError(this, new.target, ConnectionError);
}
}
// The WebLN provider doesn't support this method
export class UnsupportedMethodError extends Error {
constructor(message: string) {
super(message);
fixError(this, new.target, UnsupportedMethodError);
}
}
// The desired node couldn't be routed to
export class RoutingError extends Error {
constructor(message: string) {
super(message);
fixError(this, new.target, RoutingError);
}
}
// An argument passed was somehow invalid (e.g. malformed invoice)
export class InvalidDataError extends Error {
constructor(message: string) {
super(message);
fixError(this, new.target, InvalidDataError);
}
}
// Something broke in the WebLN provider internally, nothing to do with the app
export class InternalError extends Error {
constructor(message: string) {
super(message);
fixError(this, new.target, InternalError);
}
}