bitcore-wallet-client
Version:
Client for bitcore-wallet-service
64 lines • 1.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.extend = exports.Errors = void 0;
const spec_1 = require("./spec");
class BwcError extends Error {
constructor() {
super();
this.message = 'Internal error';
this.stack = this.message + '\n' + new Error().stack;
}
}
;
BwcError.prototype.name = 'bwc.Error';
function format(message, args) {
return message
.replace('{0}', args[0])
.replace('{1}', args[1])
.replace('{2}', args[2]);
}
;
function traverseNode(parent, errorDefinition) {
let messageHandler;
if (typeof errorDefinition.message === 'string') {
messageHandler = (args) => format(errorDefinition.message, args);
}
else if (typeof errorDefinition.message === 'function') {
messageHandler = errorDefinition.message;
}
else {
throw new Error('Invalid error definition for ' + errorDefinition.name);
}
class NodeError extends parent {
constructor(...args) {
super();
this.message = messageHandler(args);
this.stack = this.message + '\n' + new Error().stack;
}
}
;
NodeError.prototype.name = parent.prototype.name + errorDefinition.name;
parent[errorDefinition.name] = NodeError;
if (errorDefinition.errors) {
childDefinitions(NodeError, errorDefinition.errors);
}
return NodeError;
}
;
function childDefinitions(parent, childDefinitions) {
for (const childDefinition of childDefinitions) {
traverseNode(parent, childDefinition);
}
}
;
function traverseRoot(parent, errorsDefinition) {
childDefinitions(parent, errorsDefinition);
return parent;
}
;
exports.Errors = traverseRoot(BwcError, spec_1.errorSpec);
const extend = function (spec) {
return traverseNode(BwcError, spec);
};
exports.extend = extend;
//# sourceMappingURL=index.js.map