UNPKG

@piiano/vault-client

Version:

Piiano Vault generated typescript client

37 lines 1.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiError = void 0; class ApiError extends Error { constructor(request, response, message) { super(formatErrorMessage(message, response.body)); // Maintains proper stack trace for where our error was thrown (only available on V8) if (Error.captureStackTrace) { Error.captureStackTrace(this, ApiError); } this.name = ApiError.name; this.url = response.url; this.status = response.status; this.statusText = response.statusText; this.body = response.body; this.request = request; // because we are extending a built-in class Object.setPrototypeOf(this, ApiError.prototype); } } exports.ApiError = ApiError; function formatErrorMessage(message, body) { if (!isVaultHTTPError(body)) { return message; } const { error_code, message: vaultMessage, context } = body; return `${message} ${error_code}: ${vaultMessage} Context: ${JSON.stringify(context, null, 2)}`; } function isVaultHTTPError(body) { return typeof body === 'object' && body !== null && 'error_code' in body && typeof body.error_code === 'string' && 'message' in body && typeof body.message === 'string' && 'context' in body && typeof body.context === 'object' && body.context !== null; } //# sourceMappingURL=ApiError.js.map