-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.ts
114 lines (100 loc) · 2.43 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
105
106
107
108
109
110
111
112
113
114
import { fmt } from "./zcli.ts";
import { components } from "./api/openapi.ts";
import { env } from "./env.ts";
/**
* A generic application error.
*/
export class AppError extends Error {
name = "AppError";
readonly exitCode: number = 1;
constructor({ message, exitCode }: { message: string; exitCode?: number }) {
super(message);
this.exitCode = exitCode ?? 1;
}
}
/**
* An error that is caused by a misconfiguration.
*/
export class ConfigError extends AppError {
readonly name = "ConfigError";
constructor(message: string) {
super({ message });
}
}
/**
* An error that is documented in the CLI docs.
*/
export class DocumentedError extends AppError {
readonly name = "DocumentedError";
constructor({ message, path }: { message: string; path: string }) {
super({
message: message +
`\n → Learn more at: ${new URL(path, env.get("PAPERSPACE_DOCS_URL"))}`,
});
}
}
/**
* An error that is caused by an invalid user input.
*/
export class ValidationError extends AppError {
readonly name = "ValidationError";
constructor(message: string) {
super({ message: `⛔ ${message}` });
}
}
/**
* An error that is caused by git.
*/
export class DegitError extends AppError {
readonly name = "DegitError";
constructor({ message, exitCode }: { message: string; exitCode?: number }) {
super({
message: `⛔ ${fmt.colors.bold("Error cloning project")}\n${message}`,
exitCode,
});
}
}
/**
* An error that is thrown by the API client.
*/
export class ApiClientError extends AppError {
readonly name = "ApiClientError";
readonly code: ApiClientErrorCode;
readonly details:
components["responses"]["error"]["content"]["application/json"]["details"];
constructor({
message,
code,
details,
}:
& Omit<
components["responses"]["error"]["content"]["application/json"],
"code"
>
& {
code: ApiClientErrorCode;
}) {
super({ message, exitCode: 1 });
this.code = code;
this.details = details;
}
}
/**
* Error codes thrown by the API client.
*/
export const API_CLIENT_ERROR_CODES = [
"PARSE_ERROR",
"BAD_REQUEST",
"INTERNAL_SERVER_ERROR",
"UNAUTHORIZED",
"FORBIDDEN",
"NOT_FOUND",
"METHOD_NOT_SUPPORTED",
"TIMEOUT",
"CONFLICT",
"PRECONDITION_FAILED",
"PAYLOAD_TOO_LARGE",
"TOO_MANY_REQUESTS",
"CLIENT_CLOSED_REQUEST",
] as const;
export type ApiClientErrorCode = typeof API_CLIENT_ERROR_CODES[number];