Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/src/messaging/error.ts
2500 views
1
/**
2
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { scrubber } from "../util/scrubbing";
8
9
export class ApplicationError extends Error {
10
constructor(readonly code: ErrorCode, readonly message: string, readonly data?: any) {
11
super(message);
12
this.data = scrubber.scrub(this.data, true);
13
}
14
15
toJson() {
16
return {
17
code: this.code,
18
message: this.message,
19
data: this.data,
20
};
21
}
22
}
23
24
export namespace ApplicationError {
25
export function hasErrorCode(e: any): e is Error & { code: ErrorCode; data?: any } {
26
return ErrorCode.is(e["code"]);
27
}
28
29
export async function notFoundToUndefined<T>(p: Promise<T>): Promise<T | undefined> {
30
try {
31
return await p;
32
} catch (e) {
33
if (hasErrorCode(e) && e.code === ErrorCodes.NOT_FOUND) {
34
return undefined;
35
}
36
throw e;
37
}
38
}
39
40
export function isUserDeletedError(e: any): boolean {
41
return hasErrorCode(e) && e.code === ErrorCodes.NOT_FOUND && e.data?.userDeleted === true;
42
}
43
}
44
45
export namespace ErrorCode {
46
export function isUserError(code: number | ErrorCode) {
47
return code >= 400 && code < 500;
48
}
49
export function is(code: any): code is ErrorCode {
50
if (typeof code !== "number") {
51
return false;
52
}
53
return Object.values(ErrorCodes).includes(code as ErrorCode);
54
}
55
}
56
57
export type ErrorCode = typeof ErrorCodes[keyof typeof ErrorCodes];
58
59
export const ErrorCodes = {
60
// 400 Unauthorized
61
BAD_REQUEST: 400 as const,
62
63
// 401 Unauthorized
64
NOT_AUTHENTICATED: 401 as const,
65
66
// 403 Forbidden
67
PERMISSION_DENIED: 403 as const,
68
69
// 404 Not Found
70
NOT_FOUND: 404 as const,
71
72
// 409 Conflict (e.g. already existing)
73
CONFLICT: 409 as const,
74
75
// 411 No User
76
NEEDS_VERIFICATION: 411 as const,
77
78
// 412 Precondition Failed
79
PRECONDITION_FAILED: 412 as const,
80
81
// 429 Too Many Requests
82
TOO_MANY_REQUESTS: 429 as const,
83
84
// 430 Repository not whitelisted (custom status code)
85
REPOSITORY_NOT_WHITELISTED: 430 as const,
86
87
// 451 Out of credits
88
PAYMENT_SPENDING_LIMIT_REACHED: 451 as const,
89
90
// 451 Error creating a subscription
91
SUBSCRIPTION_ERROR: 452 as const,
92
93
// 455 Invalid cost center (custom status code)
94
INVALID_COST_CENTER: 455 as const,
95
96
// 460 Context Parse Error (custom status code)
97
CONTEXT_PARSE_ERROR: 460 as const,
98
99
// 461 Invalid gitpod yml (custom status code)
100
INVALID_GITPOD_YML: 461 as const,
101
102
// 470 User Blocked (custom status code)
103
USER_BLOCKED: 470 as const,
104
105
// 472 Terms Acceptance Required (custom status code)
106
USER_TERMS_ACCEPTANCE_REQUIRED: 472 as const,
107
108
// 481 Professional plan is required for this operation
109
PLAN_PROFESSIONAL_REQUIRED: 481 as const,
110
111
// 482 Cell Expired
112
CELL_EXPIRED: 482 as const,
113
114
// 490 Too Many Running Workspace
115
TOO_MANY_RUNNING_WORKSPACES: 490 as const,
116
117
// 498 The operation was cancelled, typically by the caller.
118
CANCELLED: 498 as const,
119
120
// 4981 The deadline expired before the operation could complete.
121
DEADLINE_EXCEEDED: 4981 as const,
122
123
// 500 Internal Server Error
124
INTERNAL_SERVER_ERROR: 500 as const,
125
126
// 501 EE Feature
127
EE_FEATURE: 501 as const,
128
129
// 521 Unimplemented
130
UNIMPLEMENTED: 521 as const,
131
132
// 555 EE License Required
133
EE_LICENSE_REQUIRED: 555 as const,
134
135
// 601 SaaS Feature
136
SAAS_FEATURE: 601 as const,
137
138
// 630 Snapshot Error
139
SNAPSHOT_ERROR: 630 as const,
140
141
// 640 Headless logs are not available (yet)
142
HEADLESS_LOG_NOT_YET_AVAILABLE: 640 as const,
143
144
// 650 Invalid Value
145
INVALID_VALUE: 650 as const,
146
};
147
148