diff --git a/src/boxerror.js b/src/boxerror.js index 1cb57f97a..0be959a15 100644 --- a/src/boxerror.js +++ b/src/boxerror.js @@ -18,16 +18,17 @@ function BoxError(reason, errorOrMessage, details) { this.name = this.constructor.name; this.reason = reason; + this.details = details || {}; + if (typeof errorOrMessage === 'undefined') { this.message = reason; } else if (typeof errorOrMessage === 'string') { this.message = errorOrMessage; - } else { - this.message = 'Internal error'; + } else { // error object + this.message = errorOrMessage.message; this.nestedError = errorOrMessage; + _.extend(this.details, errorOrMessage); // copy enumerable properies } - - this.details = details || {}; } util.inherits(BoxError, Error); BoxError.ACCESS_DENIED = 'Access Denied'; diff --git a/src/docker.js b/src/docker.js index ef35f4fc0..29f40754c 100644 --- a/src/docker.js +++ b/src/docker.js @@ -84,8 +84,8 @@ function ping(callback) { var docker = connectionInstance(1000); docker.ping(function (error, result) { - if (error) return callback(new BoxError(BoxError.INTERNAL_ERROR, error)); - if (result !== 'OK') return callback(new BoxError(BoxError.INTERNAL_ERROR, 'Unable to ping the docker daemon')); + if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, error)); + if (result !== 'OK') return callback(new BoxError(BoxError.DOCKER_ERROR, 'Unable to ping the docker daemon')); callback(null); }); @@ -97,7 +97,7 @@ function pullImage(manifest, callback) { // Use docker CLI here to support downloading of private repos. for dockerode, we have to use // https://github.com/apocas/dockerode#pull-from-private-repos docker.pull(manifest.dockerImage, function (error, stream) { - if (error) return callback(new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to pull image. statusCode: ' + error.statusCode)); + if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, 'Unable to pull image. statusCode: ' + error.statusCode)); // https://github.com/dotcloud/docker/issues/1074 says each status message // is emitted as a chunk @@ -120,7 +120,7 @@ function pullImage(manifest, callback) { stream.on('error', function (error) { debug('error pulling image %s of %s: %j', manifest.dockerImage, manifest.id, error); - callback(new BoxError(BoxError.EXTERNAL_ERROR, error.message)); + callback(new BoxError(BoxError.DOCKER_ERROR, error.message)); }); }); } @@ -298,7 +298,7 @@ function startContainer(containerId, callback) { container.start(function (error) { if (error && error.statusCode === 404) return callback(new BoxError(BoxError.NOT_FOUND)); - if (error && error.statusCode !== 304) return callback(new BoxError(BoxError.INTERNAL_ERROR, error)); + if (error && error.statusCode !== 304) return callback(new BoxError(BoxError.DOCKER_ERROR, error)); return callback(null); }); @@ -374,7 +374,7 @@ function deleteContainers(appId, options, callback) { if (options.managedOnly) labels.push('isCloudronManaged=true'); docker.listContainers({ all: 1, filters: JSON.stringify({ label: labels }) }, function (error, containers) { - if (error) return callback(error); + if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, error)); async.eachSeries(containers, function (container, iteratorDone) { deleteContainer(container.Id, iteratorDone); @@ -391,7 +391,7 @@ function stopContainers(appId, callback) { debug('stopping containers of %s', appId); docker.listContainers({ all: 1, filters: JSON.stringify({ label: [ 'appId=' + appId ] }) }, function (error, containers) { - if (error) return callback(error); + if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, error)); async.eachSeries(containers, function (container, iteratorDone) { stopContainer(container.Id, iteratorDone); @@ -435,7 +435,7 @@ function getContainerIdByIp(ip, callback) { docker.getNetwork('cloudron').inspect(function (error, bridge) { if (error && error.statusCode === 404) return callback(new Error('Unable to find the cloudron network')); - if (error) return callback(error); + if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, error)); var containerId; for (var id in bridge.Containers) { @@ -458,7 +458,7 @@ function inspect(containerId, callback) { container.inspect(function (error, result) { if (error && error.statusCode === 404) return callback(new BoxError(BoxError.NOT_FOUND)); - if (error) return callback(new BoxError(BoxError.INTERNAL_ERROR, error)); + if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, error)); callback(null, result); }); @@ -471,7 +471,7 @@ function getEvents(options, callback) { let docker = exports.connection; docker.getEvents(options, function (error, stream) { - if (error) return callback(new BoxError(BoxError.INTERNAL_ERROR, error)); + if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, error)); callback(null, stream); }); @@ -485,7 +485,7 @@ function memoryUsage(containerId, callback) { container.stats({ stream: false }, function (error, result) { if (error && error.statusCode === 404) return callback(new BoxError(BoxError.NOT_FOUND)); - if (error) return callback(new BoxError(BoxError.INTERNAL_ERROR, error)); + if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, error)); callback(null, result); }); @@ -549,7 +549,7 @@ function createVolume(app, name, volumeDataDir, callback) { if (error) return callback(new Error(`Error creating app data dir: ${error.message}`)); docker.createVolume(volumeOptions, function (error) { - if (error) return callback(error); + if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, error)); callback(); }); @@ -566,7 +566,7 @@ function clearVolume(app, name, options, callback) { let volume = docker.getVolume(name); volume.inspect(function (error, v) { if (error && error.statusCode === 404) return callback(); - if (error) return callback(error); + if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, error)); const volumeDataDir = v.Options.device; shell.sudo('clearVolume', [ CLEARVOLUME_CMD, options.removeDirectory ? 'rmdir' : 'clear', volumeDataDir ], {}, callback); @@ -595,7 +595,7 @@ function info(callback) { let docker = exports.connection; docker.info(function (error, result) { - if (error) return callback(new BoxError(BoxError.EXTERNAL_ERROR, 'Error connecting to docker. statusCode: ' + error.statusCode)); + if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, 'Error connecting to docker')); callback(null, result); });