|
| 1 | +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +import { castToError } from '../internal/errors'; |
| 4 | + |
| 5 | +export class LightswitchError extends Error {} |
| 6 | + |
| 7 | +export class APIError< |
| 8 | + TStatus extends number | undefined = number | undefined, |
| 9 | + THeaders extends Headers | undefined = Headers | undefined, |
| 10 | + TError extends Object | undefined = Object | undefined, |
| 11 | +> extends LightswitchError { |
| 12 | + /** HTTP status for the response that caused the error */ |
| 13 | + readonly status: TStatus; |
| 14 | + /** HTTP headers for the response that caused the error */ |
| 15 | + readonly headers: THeaders; |
| 16 | + /** JSON body of the response that caused the error */ |
| 17 | + readonly error: TError; |
| 18 | + |
| 19 | + constructor(status: TStatus, error: TError, message: string | undefined, headers: THeaders) { |
| 20 | + super(`${APIError.makeMessage(status, error, message)}`); |
| 21 | + this.status = status; |
| 22 | + this.headers = headers; |
| 23 | + this.error = error; |
| 24 | + } |
| 25 | + |
| 26 | + private static makeMessage(status: number | undefined, error: any, message: string | undefined) { |
| 27 | + const msg = |
| 28 | + error?.message ? |
| 29 | + typeof error.message === 'string' ? |
| 30 | + error.message |
| 31 | + : JSON.stringify(error.message) |
| 32 | + : error ? JSON.stringify(error) |
| 33 | + : message; |
| 34 | + |
| 35 | + if (status && msg) { |
| 36 | + return `${status} ${msg}`; |
| 37 | + } |
| 38 | + if (status) { |
| 39 | + return `${status} status code (no body)`; |
| 40 | + } |
| 41 | + if (msg) { |
| 42 | + return msg; |
| 43 | + } |
| 44 | + return '(no status code or body)'; |
| 45 | + } |
| 46 | + |
| 47 | + static generate( |
| 48 | + status: number | undefined, |
| 49 | + errorResponse: Object | undefined, |
| 50 | + message: string | undefined, |
| 51 | + headers: Headers | undefined, |
| 52 | + ): APIError { |
| 53 | + if (!status || !headers) { |
| 54 | + return new APIConnectionError({ message, cause: castToError(errorResponse) }); |
| 55 | + } |
| 56 | + |
| 57 | + const error = errorResponse as Record<string, any>; |
| 58 | + |
| 59 | + if (status === 400) { |
| 60 | + return new BadRequestError(status, error, message, headers); |
| 61 | + } |
| 62 | + |
| 63 | + if (status === 401) { |
| 64 | + return new AuthenticationError(status, error, message, headers); |
| 65 | + } |
| 66 | + |
| 67 | + if (status === 403) { |
| 68 | + return new PermissionDeniedError(status, error, message, headers); |
| 69 | + } |
| 70 | + |
| 71 | + if (status === 404) { |
| 72 | + return new NotFoundError(status, error, message, headers); |
| 73 | + } |
| 74 | + |
| 75 | + if (status === 409) { |
| 76 | + return new ConflictError(status, error, message, headers); |
| 77 | + } |
| 78 | + |
| 79 | + if (status === 422) { |
| 80 | + return new UnprocessableEntityError(status, error, message, headers); |
| 81 | + } |
| 82 | + |
| 83 | + if (status === 429) { |
| 84 | + return new RateLimitError(status, error, message, headers); |
| 85 | + } |
| 86 | + |
| 87 | + if (status >= 500) { |
| 88 | + return new InternalServerError(status, error, message, headers); |
| 89 | + } |
| 90 | + |
| 91 | + return new APIError(status, error, message, headers); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +export class APIUserAbortError extends APIError<undefined, undefined, undefined> { |
| 96 | + constructor({ message }: { message?: string } = {}) { |
| 97 | + super(undefined, undefined, message || 'Request was aborted.', undefined); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export class APIConnectionError extends APIError<undefined, undefined, undefined> { |
| 102 | + constructor({ message, cause }: { message?: string | undefined; cause?: Error | undefined }) { |
| 103 | + super(undefined, undefined, message || 'Connection error.', undefined); |
| 104 | + // in some environments the 'cause' property is already declared |
| 105 | + // @ts-ignore |
| 106 | + if (cause) this.cause = cause; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +export class APIConnectionTimeoutError extends APIConnectionError { |
| 111 | + constructor({ message }: { message?: string } = {}) { |
| 112 | + super({ message: message ?? 'Request timed out.' }); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +export class BadRequestError extends APIError<400, Headers> {} |
| 117 | + |
| 118 | +export class AuthenticationError extends APIError<401, Headers> {} |
| 119 | + |
| 120 | +export class PermissionDeniedError extends APIError<403, Headers> {} |
| 121 | + |
| 122 | +export class NotFoundError extends APIError<404, Headers> {} |
| 123 | + |
| 124 | +export class ConflictError extends APIError<409, Headers> {} |
| 125 | + |
| 126 | +export class UnprocessableEntityError extends APIError<422, Headers> {} |
| 127 | + |
| 128 | +export class RateLimitError extends APIError<429, Headers> {} |
| 129 | + |
| 130 | +export class InternalServerError extends APIError<number, Headers> {} |
0 commit comments