UNPKG

@contract-case/case-core

Version:

Core functionality for the ContractCase contract testing suite

47 lines 2.55 kB
"use strict"; // This module knows how to turn axios responses into // the relevant response objects or errors var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.unmarshallFailure = exports.unmarshallSuccess = void 0; const axios_1 = __importDefault(require("axios")); const case_plugin_base_1 = require("@contract-case/case-plugin-base"); const pretty_format_1 = require("pretty-format"); const core_1 = require("../../../core"); const apiErrors_1 = require("./apiErrors"); const unmarshallSuccess = (response) => response.data; exports.unmarshallSuccess = unmarshallSuccess; const unmarshallFailure = (error) => { if (axios_1.default.isAxiosError(error)) { if (error.response) { if (error.response.status === 403) { throw new core_1.BrokerError(`The access token you provided was rejected by the broker\n\n - To assist in debugging, the response body was:\n${(0, pretty_format_1.format)(error.response.data, { indent: 4 })}`, apiErrors_1.API_NOT_AUTHORISED); } if (error.response.data && error.response.data.errors) { let errorMessage; try { errorMessage = [ `The broker returned an error code (${error.response.status})`, ] .concat(Object.entries(error.response.data.errors) .map(([k, v]) => v.map((message) => `Error in field '${k}': ${message}`)) .flat()) .join('\n'); } catch { errorMessage = `The broker returned an error code (${error.response.status}), and failed to parse the error response`; } throw new core_1.BrokerError(errorMessage, apiErrors_1.API_ERROR); } throw new core_1.BrokerError(`The broker returned an error code (${error.response.status}): ${JSON.stringify(error.response.data)}`, apiErrors_1.API_ERROR); } if (error.request) { throw new core_1.BrokerError(`The server didn't respond: ${error.message} `, apiErrors_1.API_NO_RESPONSE); } } throw new case_plugin_base_1.CaseCoreError(`Broker API Failed with: ${error.message}\n--Original stack trace:${error.stack}`); }; exports.unmarshallFailure = unmarshallFailure; //# sourceMappingURL=marshaller.js.map