-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy patherror.ts
More file actions
94 lines (77 loc) · 2.84 KB
/
error.ts
File metadata and controls
94 lines (77 loc) · 2.84 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
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
import { VRChat } from "./client";
import { client } from "./generated/client.gen";
import { tryJsonParse } from "./lib/json";
/**
* @internal
*/
interface VRChatErrorOptions {
message: string;
response: Response;
request: Request;
}
export class VRChatError extends Error {
public request: Request;
public response: Response;
public get statusCode(): number {
return this.response.status ?? 0;
}
public get unauthorized(): boolean {
return this.statusCode === 401;
}
/**
* Creates a new `VRChatError` instance from a value.
*
* This method is used to convert various types of error values into a consistent `VRChatError` instance.
* It can handle `Error` instances, strings, and objects with a `message` property.
* If the value is already a `VRChatError`, it will return it as is.
*
* @internal
*/
public static from(value: unknown, { request, response }: Partial<Pick<VRChatErrorOptions, "request" | "response">> = {}): VRChatError {
if (value instanceof VRChatError)
return value;
request ??= new Request(client.getConfig().baseUrl!);
response ??= new Response();
if (value instanceof Error)
return new VRChatError({ message: value.message, response, request, cause: value });
if (typeof value == "string")
return new VRChatError({ message: value, response, request });
if (typeof value == "object" && value) {
if ("message" in value && typeof value.message === "string")
return new VRChatError({ message: value.message, response, request, cause: value });
if ("error" in value && typeof value.error === "object" && value.error) {
if ("message" in value.error && typeof value.error.message === "string") {
// VRChat sometimes JSON-encodes the error message, so we try to parse it, then stringify it.
return new VRChatError({ message: String(tryJsonParse(value.error.message)), response, request, cause: value });
}
}
}
// Last resort, try to stringify the reason.
return new VRChatError({ message: JSON.stringify(value), response, request, cause: value });
}
private constructor({ message, request, response, cause }: { cause?: unknown } & VRChatErrorOptions) {
super(message, { cause });
this.request = request;
this.response = response;
this.name = "VRChatError";
Error.captureStackTrace(this, VRChat);
}
public toResponseContent(): { error: { message: string; status_code: number } } {
return {
error: {
message: this.message,
status_code: this.statusCode,
}
};
}
}
export function fail({ message, request, response, throwOnError }: { throwOnError?: boolean } & VRChatErrorOptions): { data: undefined; error: VRChatError; request: Request; response: Response } {
const error = VRChatError.from(message, { request, response });
if (throwOnError) throw error;
return {
data: undefined,
error,
request: error.request,
response: error.response
};
}