This repository has been archived on 2020-01-20. You can view files and clone it, but cannot push or open issues or pull requests.
node-spearhead/lib/errors.js

149 lines
3.3 KiB
JavaScript
Raw Normal View History

2015-09-04 21:12:20 +03:00
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright 2015 Joyent, Inc.
2014-02-07 23:21:24 +02:00
*
* Error classes that the joyent CLI may produce.
*/
var util = require('util'),
format = util.format;
var assert = require('assert-plus');
var verror = require('verror'),
VError = verror.VError;
// ---- error classes
/**
* Base error. Instances will always have a string `message` and
* a string `code` (a CamelCase string).
*/
2015-08-26 00:25:30 +03:00
function TritonError(options) {
2014-02-07 23:21:24 +02:00
assert.object(options, 'options');
assert.string(options.message, 'options.message');
assert.string(options.code, 'options.code');
assert.optionalObject(options.cause, 'options.cause');
assert.optionalNumber(options.statusCode, 'options.statusCode');
var self = this;
var args = [];
if (options.cause) args.push(options.cause);
args.push(options.message);
2015-09-08 19:55:48 +03:00
VError.apply(this, args);
2014-02-07 23:21:24 +02:00
var extra = Object.keys(options).filter(
function (k) { return ['cause', 'message'].indexOf(k) === -1; });
extra.forEach(function (k) {
self[k] = options[k];
});
}
2015-08-26 00:25:30 +03:00
util.inherits(TritonError, VError);
2014-02-07 23:21:24 +02:00
function InternalError(cause, message) {
if (message === undefined) {
message = cause;
cause = undefined;
}
assert.string(message);
2015-08-26 00:25:30 +03:00
TritonError.call(this, {
2014-02-07 23:21:24 +02:00
cause: cause,
message: message,
code: 'InternalError',
exitStatus: 1
});
}
2015-08-26 00:25:30 +03:00
util.inherits(InternalError, TritonError);
2014-02-07 23:21:24 +02:00
2014-02-20 05:49:49 +02:00
2015-07-26 08:45:20 +03:00
/**
* CLI usage error
*/
function ConfigError(cause, message) {
if (message === undefined) {
message = cause;
cause = undefined;
}
assert.string(message);
2015-08-26 00:25:30 +03:00
TritonError.call(this, {
2015-07-26 08:45:20 +03:00
cause: cause,
message: message,
code: 'Config',
exitStatus: 1
});
}
2015-08-26 00:25:30 +03:00
util.inherits(ConfigError, TritonError);
2015-07-26 08:45:20 +03:00
2014-02-20 05:49:49 +02:00
/**
* CLI usage error
*/
2014-02-07 23:21:24 +02:00
function UsageError(cause, message) {
if (message === undefined) {
message = cause;
cause = undefined;
}
assert.string(message);
2015-08-26 00:25:30 +03:00
TritonError.call(this, {
2014-02-07 23:21:24 +02:00
cause: cause,
message: message,
code: 'Usage',
exitStatus: 1
});
}
2015-08-26 00:25:30 +03:00
util.inherits(UsageError, TritonError);
2014-02-20 05:49:49 +02:00
/**
* An error signing a request.
*/
function SigningError(cause) {
2015-08-26 00:25:30 +03:00
TritonError.call(this, {
2014-02-20 05:49:49 +02:00
cause: cause,
message: 'error signing request',
code: 'Signing',
exitStatus: 1
});
}
2015-08-26 00:25:30 +03:00
util.inherits(SigningError, TritonError);
2014-02-07 23:21:24 +02:00
/**
* Multiple errors in a group.
*/
function MultiError(errs) {
assert.arrayOfObject(errs, 'errs');
var lines = [format('multiple (%d) errors', errs.length)];
for (var i = 0; i < errs.length; i++) {
var err = errs[i];
lines.push(format(' error (%s): %s', err.code, err.message));
}
2015-08-26 00:25:30 +03:00
TritonError.call(this, {
2014-02-07 23:21:24 +02:00
cause: errs[0],
message: lines.join('\n'),
code: 'MultiError',
exitStatus: 1
});
}
MultiError.description = 'Multiple errors.';
2015-08-26 00:25:30 +03:00
util.inherits(MultiError, TritonError);
2014-02-07 23:21:24 +02:00
// ---- exports
module.exports = {
2015-08-26 00:25:30 +03:00
TritonError: TritonError,
2014-02-07 23:21:24 +02:00
InternalError: InternalError,
2015-07-26 08:45:20 +03:00
ConfigError: ConfigError,
2014-02-07 23:21:24 +02:00
UsageError: UsageError,
2014-02-20 05:49:49 +02:00
SigningError: SigningError,
2014-02-07 23:21:24 +02:00
MultiError: MultiError
};
// vim: set softtabstop=4 shiftwidth=4: