-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.ts
121 lines (99 loc) · 3.14 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
111
112
113
114
115
116
117
118
119
120
121
import type { InternalConfiguration, UrbexErrorType, UrbexResponse } from "../exportable-types";
interface ErrorInstanceBinding {
config: InternalConfiguration;
request: any;
}
function replaceCallStackWithName(stack: string, name: string): string {
return stack.replace(/^Error/, name);
}
/**
* Base error class for Urbex that extends the native Error class.
*/
export class UrbexError extends Error implements UrbexErrorType {
name: string = "UrbexError";
status: number;
request: any;
config: InternalConfiguration<any>;
response: UrbexResponse<any>;
constructor(message?: string) {
if (!message) {
message = "An error occurred while executing a request.";
}
super(message);
}
static create<T extends typeof UrbexError>(
this: T,
config?: InternalConfiguration
): InstanceType<T> {
const error = new this();
error.config = config;
error.name = this.name;
return error as InstanceType<T>;
}
static createErrorInstance<T extends typeof UrbexError>(
this: ErrorInstanceBinding,
instance: T
): InstanceType<T> {
const error = instance.create.call(instance, this.config);
error.request = this.request;
return error as InstanceType<T>;
}
static createFromError<T extends typeof UrbexError>(this: T, error: Error): InstanceType<T> {
if (!(error instanceof Error)) {
error = new Error(error);
}
const instance = new this(error.message);
if (error.stack) {
instance.stack = replaceCallStackWithName(error.stack, this.name);
}
instance.name = this.name;
if (UrbexError.isInstance(error)) {
instance.name = error.name;
}
return instance as InstanceType<T>;
}
static isInstance<T extends typeof UrbexError>(error: any): error is InstanceType<T> {
return error instanceof UrbexError;
}
public toJSON(): UrbexErrorType {
return {
name: this.name,
message: this.message,
status: this.status,
request: this.request,
config: this.config,
response: this.response,
stack: this.stack
};
}
}
/**
* A TimeoutError is thrown when the request takes longer than the specified timeout.
*/
export class TimeoutError extends UrbexError {
constructor(message?: string) {
super();
this.name = "TimeoutError";
this.message = message || "The request timed out.";
}
}
/**
* A NetworkError is thrown when the request fails to reach the server.
*/
export class NetworkError extends UrbexError {
constructor(message?: string) {
super();
this.name = "NetworkError";
this.message = message || "Failed to request the resource.";
}
}
/**
* A PipelineError is thrown when executing a pipeline fails.
*/
export class PipelineError extends UrbexError {
constructor(message?: string) {
super();
this.name = "PipelineError";
this.message = message || "An error occurred while executing a pipeline.";
}
}