Skip to content

Commit

Permalink
refactor: switch to template strings for errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Jul 17, 2017
1 parent 7932a25 commit 3af0141
Show file tree
Hide file tree
Showing 34 changed files with 110 additions and 109 deletions.
4 changes: 2 additions & 2 deletions lib/bip70/paymentrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ PaymentRequest.prototype.getAlgorithm = function getAlgorithm() {
throw new Error('Could not parse PKI algorithm.');

if (parts[0] !== 'x509')
throw new Error('Unknown PKI type: ' + parts[0]);
throw new Error(`Unknown PKI type: ${parts[0]}.`);

if (parts[1] !== 'sha1' && parts[1] !== 'sha256')
throw new Error('Unknown hash algorithm: ' + parts[1]);
throw new Error(`Unknown hash algorithm: ${parts[1]}.`);

return new Algorithm(parts[0], parts[1]);
};
Expand Down
8 changes: 4 additions & 4 deletions lib/bip70/x509.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ x509.getKeyAlgorithm = function getKeyAlgorithm(cert) {
let alg = x509.oid[oid];

if (!alg)
throw new Error('Unknown key algorithm: ' + oid);
throw new Error(`Unknown key algorithm: ${oid}.`);

return alg;
};
Expand All @@ -205,7 +205,7 @@ x509.getSigAlgorithm = function getSigAlgorithm(cert) {
let alg = x509.oid[oid];

if (!alg || !alg.hash)
throw new Error('Unknown signature algorithm: ' + oid);
throw new Error(`Unknown signature algorithm: ${oid}.`);

return alg;
};
Expand All @@ -228,7 +228,7 @@ x509.getCurve = function getCurve(params) {
curve = x509.curves[oid];

if (!curve)
throw new Error('Unknown ECDSA curve: ' + oid);
throw new Error(`Unknown ECDSA curve: ${oid}.`);

return curve;
};
Expand Down Expand Up @@ -457,7 +457,7 @@ x509.verifyChain = function verifyChain(certs) {
let sig = child.sig;

if (!pk.verify(alg.hash, msg, sig, key))
throw new Error(alg.key + ' verification failed for chain.');
throw new Error(`${alg.key} verification failed for chain.`);
}

// Make sure we trust one
Expand Down
2 changes: 1 addition & 1 deletion lib/blockchain/chaindb.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ ChainDB.prototype.prune = async function prune(tip) {
let hash = await this.getHash(i);

if (!hash)
throw new Error('Cannot find hash for ' + i);
throw new Error(`Cannot find hash for ${i}.`);

batch.del(layout.b(hash));
batch.del(layout.u(hash));
Expand Down
4 changes: 2 additions & 2 deletions lib/btc/amount.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Amount.prototype.to = function to(unit, num) {
case 'btc':
return this.toBTC(num);
}
throw new Error('Unknown unit "' + unit + '".');
throw new Error(`Unknown unit "${unit}".`);
};

/**
Expand Down Expand Up @@ -218,7 +218,7 @@ Amount.prototype.from = function from(unit, value, num) {
case 'btc':
return this.fromBTC(value, num);
}
throw new Error('Unknown unit "' + unit + '".');
throw new Error(`Unknown unit "${unit}".`);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/crypto/pbkdf2-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ function getHash(name) {
case 'sha512':
return 'SHA-512';
default:
throw new Error('Algorithm not supported: ' + name);
throw new Error(`Algorithm not supported: ${name}.`);
}
}
4 changes: 2 additions & 2 deletions lib/db/backends.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ exports.get = function get(name) {
case 'memory':
return require('./memdb');
default:
throw new Error('Database backend "' + name + '" not found.');
throw new Error(`Database backend "${name}" not found.`);
}
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND')
throw new Error('Database backend "' + name + '" not found.');
throw new Error(`Database backend "${name}" not found.`);
throw e;
}
};
2 changes: 1 addition & 1 deletion lib/hd/wordlist-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ exports.get = function get(name) {
case 'spanish':
return words.spanish;
default:
throw new Error('Unknown language: ' + name);
throw new Error(`Unknown language: ${name}.`);
}
};
2 changes: 1 addition & 1 deletion lib/hd/wordlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ exports.get = function get(name) {
case 'spanish':
return require('./words/spanish.js');
default:
throw new Error('Unknown language: ' + name);
throw new Error(`Unknown language: ${name}.`);
}
};
11 changes: 5 additions & 6 deletions lib/http/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ HTTPBase.prototype._init = function _init() {
this.server.on('connection', (socket) => {
socket.on('error', (err) => {
if (err.message === 'Parse Error') {
let msg = 'http_parser.execute failure (';
msg += 'parsed=' + (err.bytesParsed || -1);
msg += ' code=' + err.code;
msg += ')';
let msg = 'http_parser.execute failure';
msg += ` (parsed=${err.bytesParsed || -1}`;
msg += ` code=${err.code})`;
err = new Error(msg);
}

Expand Down Expand Up @@ -134,7 +133,7 @@ HTTPBase.prototype.handleRequest = async function handleRequest(req, res) {
routes = this.routes.getHandlers(req.method);

if (!routes)
throw new Error('No routes found for method: ' + req.method);
throw new Error(`No routes found for method: ${req.method}.`);

for (let route of routes) {
let params = route.match(req.pathname);
Expand All @@ -151,7 +150,7 @@ HTTPBase.prototype.handleRequest = async function handleRequest(req, res) {
return;
}

throw new Error('No routes found for path: ' + req.pathname);
throw new Error(`No routes found for path: ${req.pathname}.`);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ function getType(type) {
case 'bin':
return 'application/octet-stream';
default:
throw new Error('Unknown type: ' + type);
throw new Error(`Unknown type: ${type}.`);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/http/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ RPC.prototype._createTemplate = async function _createTemplate(maxVersion, coinb
if (!deploy.force) {
if (!rules || rules.indexOf(name) === -1) {
throw new RPCError(errs.INVALID_PARAMETER,
'Client must support ' + name + '.');
`Client must support ${name}.`);
}
name = '!' + name;
}
Expand Down Expand Up @@ -2398,7 +2398,7 @@ RPC.prototype._addBlock = async function _addBlock(block) {
if (err.type === 'VerifyError') {
this.logger.warning('RPC block rejected: %s (%s).',
block.rhash(), err.reason);
return 'rejected: ' + err.reason;
return `rejected: ${err.reason}`;
}
throw err;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/http/rpcbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ RPCBase.prototype.execute = async function execute(json, help) {
return await mount.execute(json, help);
}
throw new RPCError(RPCBase.errors.METHOD_NOT_FOUND,
'Method not found: ' + json.method + '.');
`Method not found: ${json.method}.`);
}

return await func.call(this, json.params, help);
Expand Down
5 changes: 3 additions & 2 deletions lib/http/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'use strict';

const assert = require('assert');
const path = require('path');
const HTTPBase = require('./base');
const util = require('../utils/util');
const base58 = require('../utils/base58');
Expand Down Expand Up @@ -774,8 +775,8 @@ HTTPOptions.prototype.fromOptions = function fromOptions(options) {
if (options.prefix != null) {
assert(typeof options.prefix === 'string');
this.prefix = options.prefix;
this.keyFile = this.prefix + '/key.pem';
this.certFile = this.prefix + '/cert.pem';
this.keyFile = path.join(this.prefix, 'key.pem');
this.certFile = path.join(this.prefix, 'cert.pem');
}

if (options.host != null) {
Expand Down
3 changes: 2 additions & 1 deletion lib/mempool/mempool.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'use strict';

const assert = require('assert');
const path = require('path');
const AsyncObject = require('../utils/asyncobject');
const common = require('../blockchain/common');
const policy = require('../protocol/policy');
Expand Down Expand Up @@ -2057,7 +2058,7 @@ MempoolOptions.prototype.fromOptions = function fromOptions(options) {
if (options.prefix != null) {
assert(typeof options.prefix === 'string');
this.prefix = options.prefix;
this.location = this.prefix + '/mempool';
this.location = path.join(this.prefix, 'mempool');
}

if (options.location != null) {
Expand Down
6 changes: 3 additions & 3 deletions lib/net/bip150.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ BIP150.prototype._wait = function wait(timeout, resolve, reject) {
this.job = co.job(resolve, reject);

if (this.outbound && !this.peerIdentity) {
this.reject(new Error('No identity for ' + this.hostname + '.'));
this.reject(new Error(`No identity for ${this.hostname}.`));
return;
}

Expand Down Expand Up @@ -729,7 +729,7 @@ AuthDB.prototype.parseKnown = function parseKnown(text) {
key = Buffer.from(key, 'hex');

if (key.length !== 33)
throw new Error('Invalid key: ' + parts[1]);
throw new Error(`Invalid key: ${parts[1]}.`);

if (host && host.length > 0)
this.addKnown(host, key);
Expand Down Expand Up @@ -792,7 +792,7 @@ AuthDB.prototype.parseAuth = function parseAuth(text) {
key = Buffer.from(line, 'hex');

if (key.length !== 33)
throw new Error('Invalid key: ' + line);
throw new Error(`Invalid key: ${line}.`);

this.addAuthorized(key);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/net/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ exports.REQUIRED_SERVICES = 0
* @default
*/

exports.USER_AGENT = '/bcoin:' + pkg.version + '/';
exports.USER_AGENT = `/bcoin:${pkg.version}/`;

/**
* Max message size (~4mb with segwit, formerly 2mb)
Expand Down
4 changes: 2 additions & 2 deletions lib/net/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1419,10 +1419,10 @@ Peer.prototype.error = function error(err) {
msg = err.code;
err = new Error(msg);
err.code = msg;
err.message = 'Socket Error: ' + msg;
err.message = `Socket Error: ${msg}`;
}

err.message += ' (' + this.hostname() + ')';
err.message += ` (${this.hostname()})`;

this.emit('error', err);
};
Expand Down
2 changes: 1 addition & 1 deletion lib/net/socks.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ function parseAddr(data, offset) {
port = br.readU16BE();
break;
default:
throw new Error('Unknown SOCKS address type: ' + type + '.');
throw new Error(`Unknown SOCKS address type: ${type}.`);
}

return {
Expand Down
2 changes: 1 addition & 1 deletion lib/net/upnp.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ function findError(el) {
if (cdesc)
desc = cdesc.text;

return new Error('UPnPError: ' + desc + ' (' + code + ')');
return new Error(`UPnPError: ${desc} (${code}).`);
}

/*
Expand Down
Loading

0 comments on commit 3af0141

Please sign in to comment.