UNPKG

as-procedure

Version:

easily create procedures within a pit-of-success

39 lines 1.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withExpectOutput = void 0; const error_fns_1 = require("@ehmpathy/error-fns"); const HasName_1 = require("../domain/HasName"); const withExpectOutput = (logic) => { const wrapped = (...args) => { // define an error which has access to the original call, so we can trace the stack to the root caller, since promises loose their chain const errorFromOriginalCall = new error_fns_1.HelpfulError('expect.output: call', { procedure: (0, HasName_1.getName)(logic), input: args[0], }); // invoke the request const promise = logic(...args); // add the expect wrapper const expect = (async (operation) => { const result = await promise; const output = result; if (operation === 'isPresent' && !output) throw new error_fns_1.HelpfulError('expect.output: breach: output is not present', { cause: errorFromOriginalCall, procedure: (0, HasName_1.getName)(logic), output, }); if (operation === 'isNull' && output) throw new error_fns_1.HelpfulError('expect.isNull failed for procedure', { cause: errorFromOriginalCall, procedure: (0, HasName_1.getName)(logic), output, }); return output; }); const extended = Object.assign(promise, { expect }); return extended; }; return wrapped; }; exports.withExpectOutput = withExpectOutput; //# sourceMappingURL=withExpectOutput.js.map