flocore/lib/util/preconditions.js
Braydon Fuller 904df59493 Added docs link to InvalidArgument message for preconditions.
- Error messages will appear as: "Invalid Argument: First argument is required, please include address data. Documentation: http://bitcore.io/guide/address.html"
2015-02-09 21:46:10 -05:00

35 lines
1.0 KiB
JavaScript

'use strict';
var errors = require('../errors');
var _ = require('lodash');
module.exports = {
checkState: function(condition, message) {
if (!condition) {
throw new errors.InvalidState(message);
}
},
checkArgument: function(condition, argumentName, message, docsPath) {
if (!condition) {
throw new errors.InvalidArgument(argumentName, message, docsPath);
}
},
checkArgumentType: function(argument, type, argumentName) {
argumentName = argumentName || '(unknown name)';
if (_.isString(type)) {
if (type === 'Buffer') {
var BufferUtil = require('./buffer');
if (!BufferUtil.isBuffer(argument)) {
throw new errors.InvalidArgumentType(argument, type, argumentName);
}
} else if (typeof argument !== type) {
throw new errors.InvalidArgumentType(argument, type, argumentName);
}
} else {
if (!(argument instanceof type)) {
throw new errors.InvalidArgumentType(argument, type.name, argumentName);
}
}
}
};