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'),
|
2015-10-07 09:09:52 +03:00
|
|
|
VError = verror.VError,
|
|
|
|
WError = verror.WError;
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---- error classes
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base error. Instances will always have a string `message` and
|
|
|
|
* a string `code` (a CamelCase string).
|
|
|
|
*/
|
2015-12-07 21:28:59 +02:00
|
|
|
function _TritonBaseVError(opts) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.message, 'opts.message');
|
|
|
|
assert.optionalString(opts.code, 'opts.code');
|
|
|
|
assert.optionalObject(opts.cause, 'opts.cause');
|
|
|
|
assert.optionalNumber(opts.statusCode, 'opts.statusCode');
|
2014-02-07 23:21:24 +02:00
|
|
|
var self = this;
|
|
|
|
|
2015-12-07 21:28:59 +02:00
|
|
|
/*
|
|
|
|
* If the given cause has `body.errors` a la
|
|
|
|
* https://github.com/joyent/eng/blob/master/docs/index.md#error-handling
|
|
|
|
* then lets add text about those specifics to the error message.
|
|
|
|
*/
|
|
|
|
var message = opts.message;
|
|
|
|
if (opts.cause && opts.cause.body && opts.cause.body.errors) {
|
|
|
|
opts.cause.body.errors.forEach(function (e) {
|
|
|
|
message += format('\n %s: %s', e.field, e.code);
|
|
|
|
if (e.message) {
|
|
|
|
message += ': ' + e.message;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var veArgs = [];
|
|
|
|
if (opts.cause) veArgs.push(opts.cause);
|
|
|
|
veArgs.push(message);
|
|
|
|
VError.apply(this, veArgs);
|
2014-02-07 23:21:24 +02:00
|
|
|
|
2015-12-07 21:28:59 +02:00
|
|
|
var extra = Object.keys(opts).filter(
|
2014-02-07 23:21:24 +02:00
|
|
|
function (k) { return ['cause', 'message'].indexOf(k) === -1; });
|
|
|
|
extra.forEach(function (k) {
|
2015-12-07 21:28:59 +02:00
|
|
|
self[k] = opts[k];
|
2014-02-07 23:21:24 +02:00
|
|
|
});
|
|
|
|
}
|
2015-10-07 09:09:52 +03:00
|
|
|
util.inherits(_TritonBaseVError, VError);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Base error class that doesn't include a 'cause' message in its message.
|
|
|
|
* This is useful in cases where we are wrapping CloudAPI errors with
|
|
|
|
* onces that should *replace* the CloudAPI error message.
|
|
|
|
*/
|
2015-12-07 21:28:59 +02:00
|
|
|
function _TritonBaseWError(opts) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.message, 'opts.message');
|
|
|
|
assert.optionalString(opts.code, 'opts.code');
|
|
|
|
assert.optionalObject(opts.cause, 'opts.cause');
|
|
|
|
assert.optionalNumber(opts.statusCode, 'opts.statusCode');
|
2015-10-07 09:09:52 +03:00
|
|
|
var self = this;
|
|
|
|
|
2015-12-07 21:28:59 +02:00
|
|
|
var weArgs = [];
|
|
|
|
if (opts.cause) weArgs.push(opts.cause);
|
|
|
|
weArgs.push(opts.message);
|
|
|
|
WError.apply(this, weArgs);
|
2015-10-07 09:09:52 +03:00
|
|
|
|
2015-12-07 21:28:59 +02:00
|
|
|
var extra = Object.keys(opts).filter(
|
2015-10-07 09:09:52 +03:00
|
|
|
function (k) { return ['cause', 'message'].indexOf(k) === -1; });
|
|
|
|
extra.forEach(function (k) {
|
2015-12-07 21:28:59 +02:00
|
|
|
self[k] = opts[k];
|
2015-10-07 09:09:52 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
util.inherits(_TritonBaseWError, WError);
|
2015-09-25 22:19:29 +03:00
|
|
|
|
2015-12-07 21:28:59 +02:00
|
|
|
|
2015-09-25 22:19:29 +03:00
|
|
|
/*
|
|
|
|
* A generic (i.e. a cop out) code-less error.
|
2015-12-07 21:28:59 +02:00
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* new TritonError(<message>)
|
|
|
|
* new TritonError(<cause>, <message>)
|
2015-09-25 22:19:29 +03:00
|
|
|
*/
|
|
|
|
function TritonError(cause, message) {
|
|
|
|
if (message === undefined) {
|
|
|
|
message = cause;
|
|
|
|
cause = undefined;
|
|
|
|
}
|
|
|
|
assert.string(message);
|
2015-10-07 09:09:52 +03:00
|
|
|
_TritonBaseVError.call(this, {
|
2015-09-25 22:19:29 +03:00
|
|
|
cause: cause,
|
|
|
|
message: message,
|
|
|
|
exitStatus: 1
|
|
|
|
});
|
|
|
|
}
|
2015-10-07 09:09:52 +03:00
|
|
|
util.inherits(TritonError, _TritonBaseVError);
|
2015-09-25 22:19:29 +03:00
|
|
|
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
function InternalError(cause, message) {
|
|
|
|
if (message === undefined) {
|
|
|
|
message = cause;
|
|
|
|
cause = undefined;
|
|
|
|
}
|
|
|
|
assert.string(message);
|
2015-10-07 09:09:52 +03:00
|
|
|
_TritonBaseVError.call(this, {
|
2014-02-07 23:21:24 +02:00
|
|
|
cause: cause,
|
|
|
|
message: message,
|
|
|
|
code: 'InternalError',
|
|
|
|
exitStatus: 1
|
|
|
|
});
|
|
|
|
}
|
2015-10-07 09:09:52 +03:00
|
|
|
util.inherits(InternalError, _TritonBaseVError);
|
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-10-07 09:09:52 +03:00
|
|
|
_TritonBaseVError.call(this, {
|
2015-07-26 08:45:20 +03:00
|
|
|
cause: cause,
|
|
|
|
message: message,
|
|
|
|
code: 'Config',
|
|
|
|
exitStatus: 1
|
|
|
|
});
|
|
|
|
}
|
2015-10-07 09:09:52 +03:00
|
|
|
util.inherits(ConfigError, _TritonBaseVError);
|
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-10-07 09:09:52 +03:00
|
|
|
_TritonBaseVError.call(this, {
|
2014-02-07 23:21:24 +02:00
|
|
|
cause: cause,
|
|
|
|
message: message,
|
|
|
|
code: 'Usage',
|
2015-10-07 09:09:52 +03:00
|
|
|
exitStatus: 2
|
2014-02-07 23:21:24 +02:00
|
|
|
});
|
|
|
|
}
|
2015-10-07 09:09:52 +03:00
|
|
|
util.inherits(UsageError, _TritonBaseVError);
|
2014-02-20 05:49:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An error signing a request.
|
|
|
|
*/
|
|
|
|
function SigningError(cause) {
|
2015-10-07 09:09:52 +03:00
|
|
|
_TritonBaseVError.call(this, {
|
2014-02-20 05:49:49 +02:00
|
|
|
cause: cause,
|
|
|
|
message: 'error signing request',
|
|
|
|
code: 'Signing',
|
|
|
|
exitStatus: 1
|
|
|
|
});
|
|
|
|
}
|
2015-10-07 09:09:52 +03:00
|
|
|
util.inherits(SigningError, _TritonBaseVError);
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
|
2015-09-24 07:08:26 +03:00
|
|
|
/**
|
|
|
|
* A 'DEPTH_ZERO_SELF_SIGNED_CERT' An error signing a request.
|
|
|
|
*/
|
|
|
|
function SelfSignedCertError(cause, url) {
|
|
|
|
var msg = format('could not access CloudAPI %s because it uses a ' +
|
|
|
|
'self-signed TLS certificate and your current profile is not ' +
|
|
|
|
'configured for insecure access', url);
|
2015-10-07 09:09:52 +03:00
|
|
|
_TritonBaseVError.call(this, {
|
2015-09-24 07:08:26 +03:00
|
|
|
cause: cause,
|
|
|
|
message: msg,
|
|
|
|
code: 'SelfSignedCert',
|
|
|
|
exitStatus: 1
|
|
|
|
});
|
|
|
|
}
|
2015-10-07 09:09:52 +03:00
|
|
|
util.inherits(SelfSignedCertError, _TritonBaseVError);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A resource (instance, image, ...) was not found.
|
|
|
|
*/
|
|
|
|
function ResourceNotFoundError(cause, msg) {
|
|
|
|
if (msg === undefined) {
|
|
|
|
msg = cause;
|
|
|
|
cause = undefined;
|
|
|
|
}
|
|
|
|
_TritonBaseWError.call(this, {
|
|
|
|
cause: cause,
|
|
|
|
message: msg,
|
|
|
|
code: 'ResourceNotFound',
|
|
|
|
exitStatus: 3
|
|
|
|
});
|
|
|
|
}
|
|
|
|
util.inherits(ResourceNotFoundError, _TritonBaseWError);
|
2015-09-24 07:08:26 +03:00
|
|
|
|
|
|
|
|
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-10-07 09:09:52 +03:00
|
|
|
_TritonBaseVError.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-10-07 09:09:52 +03:00
|
|
|
util.inherits(MultiError, _TritonBaseVError);
|
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,
|
2015-09-24 07:08:26 +03:00
|
|
|
SelfSignedCertError: SelfSignedCertError,
|
2015-10-07 09:09:52 +03:00
|
|
|
ResourceNotFoundError: ResourceNotFoundError,
|
2014-02-07 23:21:24 +02:00
|
|
|
MultiError: MultiError
|
|
|
|
};
|
|
|
|
// vim: set softtabstop=4 shiftwidth=4:
|