Path: blob/main/components/gitpod-protocol/src/messaging/error.ts
2500 views
/**1* Copyright (c) 2020 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { scrubber } from "../util/scrubbing";78export class ApplicationError extends Error {9constructor(readonly code: ErrorCode, readonly message: string, readonly data?: any) {10super(message);11this.data = scrubber.scrub(this.data, true);12}1314toJson() {15return {16code: this.code,17message: this.message,18data: this.data,19};20}21}2223export namespace ApplicationError {24export function hasErrorCode(e: any): e is Error & { code: ErrorCode; data?: any } {25return ErrorCode.is(e["code"]);26}2728export async function notFoundToUndefined<T>(p: Promise<T>): Promise<T | undefined> {29try {30return await p;31} catch (e) {32if (hasErrorCode(e) && e.code === ErrorCodes.NOT_FOUND) {33return undefined;34}35throw e;36}37}3839export function isUserDeletedError(e: any): boolean {40return hasErrorCode(e) && e.code === ErrorCodes.NOT_FOUND && e.data?.userDeleted === true;41}42}4344export namespace ErrorCode {45export function isUserError(code: number | ErrorCode) {46return code >= 400 && code < 500;47}48export function is(code: any): code is ErrorCode {49if (typeof code !== "number") {50return false;51}52return Object.values(ErrorCodes).includes(code as ErrorCode);53}54}5556export type ErrorCode = typeof ErrorCodes[keyof typeof ErrorCodes];5758export const ErrorCodes = {59// 400 Unauthorized60BAD_REQUEST: 400 as const,6162// 401 Unauthorized63NOT_AUTHENTICATED: 401 as const,6465// 403 Forbidden66PERMISSION_DENIED: 403 as const,6768// 404 Not Found69NOT_FOUND: 404 as const,7071// 409 Conflict (e.g. already existing)72CONFLICT: 409 as const,7374// 411 No User75NEEDS_VERIFICATION: 411 as const,7677// 412 Precondition Failed78PRECONDITION_FAILED: 412 as const,7980// 429 Too Many Requests81TOO_MANY_REQUESTS: 429 as const,8283// 430 Repository not whitelisted (custom status code)84REPOSITORY_NOT_WHITELISTED: 430 as const,8586// 451 Out of credits87PAYMENT_SPENDING_LIMIT_REACHED: 451 as const,8889// 451 Error creating a subscription90SUBSCRIPTION_ERROR: 452 as const,9192// 455 Invalid cost center (custom status code)93INVALID_COST_CENTER: 455 as const,9495// 460 Context Parse Error (custom status code)96CONTEXT_PARSE_ERROR: 460 as const,9798// 461 Invalid gitpod yml (custom status code)99INVALID_GITPOD_YML: 461 as const,100101// 470 User Blocked (custom status code)102USER_BLOCKED: 470 as const,103104// 472 Terms Acceptance Required (custom status code)105USER_TERMS_ACCEPTANCE_REQUIRED: 472 as const,106107// 481 Professional plan is required for this operation108PLAN_PROFESSIONAL_REQUIRED: 481 as const,109110// 482 Cell Expired111CELL_EXPIRED: 482 as const,112113// 490 Too Many Running Workspace114TOO_MANY_RUNNING_WORKSPACES: 490 as const,115116// 498 The operation was cancelled, typically by the caller.117CANCELLED: 498 as const,118119// 4981 The deadline expired before the operation could complete.120DEADLINE_EXCEEDED: 4981 as const,121122// 500 Internal Server Error123INTERNAL_SERVER_ERROR: 500 as const,124125// 501 EE Feature126EE_FEATURE: 501 as const,127128// 521 Unimplemented129UNIMPLEMENTED: 521 as const,130131// 555 EE License Required132EE_LICENSE_REQUIRED: 555 as const,133134// 601 SaaS Feature135SAAS_FEATURE: 601 as const,136137// 630 Snapshot Error138SNAPSHOT_ERROR: 630 as const,139140// 640 Headless logs are not available (yet)141HEADLESS_LOG_NOT_YET_AVAILABLE: 640 as const,142143// 650 Invalid Value144INVALID_VALUE: 650 as const,145};146147148