as-procedure
Version:
easily create procedures within a pit-of-success
39 lines • 1.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withExpectOutput = void 0;
const helpful_errors_1 = require("helpful-errors");
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 helpful_errors_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 helpful_errors_1.HelpfulError('expect.output: breach: output is not present', {
cause: errorFromOriginalCall,
procedure: (0, HasName_1.getName)(logic),
output,
});
if (operation === 'isNull' && output)
throw new helpful_errors_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