UNPKG

magicbell

Version:
120 lines 3.7 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.NotFoundError = exports.UnknownError = exports.IdempotencyError = exports.ConnectionError = exports.RateLimitError = exports.PermissionError = exports.AuthenticationError = exports.APIError = exports.UserInputError = exports.InvalidRequestError = exports.MagicBellError = void 0; exports.createError = createError; function createError(rawError) { for (const field of ['code', 'type', 'status']) { switch (rawError[field]) { case 'user_identifier_not_provided': return new AuthenticationError(rawError); case 'invalid_request_error': return new InvalidRequestError(rawError); case 'api_error': return new APIError(rawError); case 'authentication_error': return new AuthenticationError(rawError); case 'rate_limit_error': return new RateLimitError(rawError); case 'idempotency_error': return new IdempotencyError(rawError); case 404: return new NotFoundError(rawError); case 422: return new UserInputError(rawError); } } return new UnknownError(rawError); } /** * BaseError is the base error from which all other more specific errors derive. * Specifically for errors returned from REST API. */ class MagicBellError extends Error { /** * The name of the error. */ name; /** * The error message returned by the REST API. */ message; /** * The type of the error. */ type; /** * The URL to the documentation for the error. */ docsUrl; /** * The error code returned by the REST API. */ code; /** * The HTTP status code returned by the REST API. */ status; /** * The HTTP status text returned by the REST API. */ statusText; /** * A suggestion on how to fix the error. */ suggestion; /** * The raw response body returned by the REST API. */ responseBody; /** * @deprecated - use docsUrl instead */ get docs_url() { return this.docsUrl; } constructor(raw) { super(raw.message); this.type = this.constructor.name; this.name = 'MagicBellError'; this.code = raw.code; this.status = raw.status; this.statusText = raw.statusText; this.responseBody = raw.responseBody; this.message = raw.message; this.suggestion = raw.suggestion; this.docsUrl = raw.docs_url || raw.help_link; this.stack = raw.stack; } } exports.MagicBellError = MagicBellError; class InvalidRequestError extends MagicBellError { } exports.InvalidRequestError = InvalidRequestError; class UserInputError extends MagicBellError { } exports.UserInputError = UserInputError; class APIError extends MagicBellError { } exports.APIError = APIError; class AuthenticationError extends MagicBellError { } exports.AuthenticationError = AuthenticationError; class PermissionError extends MagicBellError { } exports.PermissionError = PermissionError; class RateLimitError extends MagicBellError { } exports.RateLimitError = RateLimitError; class ConnectionError extends MagicBellError { } exports.ConnectionError = ConnectionError; class IdempotencyError extends MagicBellError { } exports.IdempotencyError = IdempotencyError; class UnknownError extends MagicBellError { } exports.UnknownError = UnknownError; class NotFoundError extends MagicBellError { } exports.NotFoundError = NotFoundError; //# sourceMappingURL=error.js.map