@pact-foundation/pact
Version:
Pact for all things Javascript
39 lines • 1.64 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createProxyStateHandler = void 0;
const chalk_1 = __importDefault(require("chalk"));
const stack_utils_1 = __importDefault(require("stack-utils"));
const setupStates_1 = require("./setupStates");
const cleanStack = (e) => {
const stack = new stack_utils_1.default({
cwd: process.cwd(),
internals: stack_utils_1.default.nodeInternals(),
});
if (!e.stack)
return '';
const cleanedStack = stack.clean(e.stack);
const lines = cleanedStack.split('\n').map((line) => line.trim());
return lines[0];
};
const createProxyStateHandler = (config) => async (req, res) => {
const state = req.body;
try {
const data = await (0, setupStates_1.setupStates)(state, config);
return res.json(data);
}
catch (e) {
const caughtError = e instanceof Error ? e : new Error(String(e));
const error = `\nError executing state handler for state '${state.state}' on '${state.action}'.`;
const errorDetails = `↳ Error details: ${caughtError.message}`;
const errorSource = `↳ Error source: ${cleanStack(caughtError)}\n`;
console.log(chalk_1.default.red(error));
console.log(chalk_1.default.red(errorDetails));
console.log(chalk_1.default.red(errorSource));
return res.status(200).send();
}
};
exports.createProxyStateHandler = createProxyStateHandler;
//# sourceMappingURL=stateHandler.js.map