diff --git a/src/addons.js b/src/addons.js index 4df163d5b..2a9b7b8a2 100644 --- a/src/addons.js +++ b/src/addons.js @@ -1125,12 +1125,13 @@ function pipeRequestToFile(url, filename, callback) { callback(error); }); - writeStream.on('error', done); + writeStream.on('error', (error) => done(new BoxError(BoxError.FS_ERROR, `Error writing to ${filename}: ${error.message}`))); + writeStream.on('open', function () { // note: do not attach to post callback handler because this will buffer the entire reponse! // see https://github.com/request/request/issues/2270 const req = request.post(url, { rejectUnauthorized: false }); - req.on('error', done); // network error, dns error, request errored in middle etc + req.on('error', (error) => done(new BoxError(BoxError.NETWORK_ERROR, `Request error writing to ${filename}: ${error.message}`))); // network error, dns error, request errored in middle etc req.on('response', function (response) { if (response.statusCode !== 200) return done(new BoxError(BoxError.ADDONS_ERROR, `Unexpected response code when piping ${url}: ${response.statusCode} message: ${response.statusMessage} filename: ${filename}`)); @@ -1171,7 +1172,7 @@ function restoreMySql(app, options, callback) { if (error) return callback(error); var input = fs.createReadStream(dumpPath('mysql', app.id)); - input.on('error', callback); + input.on('error', (error) => callback(new BoxError(BoxError.FS_ERROR, `Error reading input stream when restoring mysql: ${error.message}`))); const restoreReq = request.post(`https://${result.ip}:3000/` + (options.multipleDatabases ? 'prefixes' : 'databases') + `/${database}/restore?access_token=${result.token}`, { json: true, rejectUnauthorized: false }, function (error, response) { if (error) return callback(new BoxError(BoxError.ADDONS_ERROR, `Error restoring mysql: ${error.message}`)); @@ -1353,7 +1354,7 @@ function restorePostgreSql(app, options, callback) { if (error) return callback(error); var input = fs.createReadStream(dumpPath('postgresql', app.id)); - input.on('error', callback); + input.on('error', (error) => callback(new BoxError(BoxError.FS_ERROR, `Error reading input stream when restoring postgresql: ${error.message}`))); const restoreReq = request.post(`https://${result.ip}:3000/databases/${database}/restore?access_token=${result.token}&username=${username}`, { json: true, rejectUnauthorized: false }, function (error, response) { if (error) return callback(new BoxError(BoxError.ADDONS_ERROR, `Error restoring postgresql: ${error.message}`)); @@ -1527,7 +1528,7 @@ function restoreMongoDb(app, options, callback) { if (error) return callback(error); const readStream = fs.createReadStream(dumpPath('mongodb', app.id)); - readStream.on('error', callback); + readStream.on('error', (error) => callback(new BoxError(BoxError.FS_ERROR, `Error reading input stream when restoring mongodb: ${error.message}`))); const restoreReq = request.post(`https://${result.ip}:3000/databases/${app.id}/restore?access_token=${result.token}`, { json: true, rejectUnauthorized: false }, function (error, response) { if (error) return callback(new BoxError(BoxError.ADDONS_ERROR, `Error restoring mongodb: ${error.message}`)); @@ -1700,6 +1701,8 @@ function restoreRedis(app, options, callback) { debugApp(app, 'Restoring redis'); + callback = once(callback); // protect from multiple returns with streams + getServiceDetails('redis-' + app.id, 'CLOUDRON_REDIS_TOKEN', function (error, result) { if (error) return callback(error); @@ -1710,7 +1713,7 @@ function restoreRedis(app, options, callback) { } else { // old location of dumps input = fs.createReadStream(path.join(paths.APPS_DATA_DIR, app.id, 'redis/dump.rdb')); } - input.on('error', callback); + input.on('error', (error) => callback(new BoxError(BoxError.FS_ERROR, `Error reading input stream when restoring redis: ${error.message}`))); const restoreReq = request.post(`https://${result.ip}:3000/restore?access_token=${result.token}`, { json: true, rejectUnauthorized: false }, function (error, response) { if (error) return callback(new BoxError(BoxError.ADDONS_ERROR, `Error restoring redis: ${error.message}`)); diff --git a/src/apps.js b/src/apps.js index af25909b1..b04559eb0 100644 --- a/src/apps.js +++ b/src/apps.js @@ -1996,7 +1996,7 @@ function uploadFile(appId, sourceFilePath, destFilePath, callback) { const done = once(function (error) { safe.fs.unlinkSync(sourceFilePath); // remove file in /tmp - callback(error); + callback(new BoxError(BoxError.FS_ERROR, error.message)); // blame it on filesystem for now }); // the built-in bash printf understands "%q" but not /usr/bin/printf. diff --git a/src/boxerror.js b/src/boxerror.js index 2b4e754f4..34714834e 100644 --- a/src/boxerror.js +++ b/src/boxerror.js @@ -88,6 +88,7 @@ BoxError.toHttpError = function (error) { case BoxError.FS_ERROR: case BoxError.MAIL_ERROR: case BoxError.DOCKER_ERROR: + case BoxError.ADDONS_ERROR: return new HttpError(424, error); case BoxError.DATABASE_ERROR: case BoxError.INTERNAL_ERROR: diff --git a/src/mail.js b/src/mail.js index 11970b701..84140113d 100644 --- a/src/mail.js +++ b/src/mail.js @@ -129,7 +129,7 @@ function checkOutboundPort25(callback) { relay.status = false; relay.value = `Connect to ${smtpServer} failed: ${error.message}. Check if port 25 (outbound) is blocked`; client.destroy(); - callback(error, relay); + callback(new BoxError(BoxError.NETWORK_ERROR, `Connect to ${smtpServer} failed.`), relay); }); } diff --git a/src/shell.js b/src/shell.js index 2f6818c8e..3bb8aa5ae 100644 --- a/src/shell.js +++ b/src/shell.js @@ -61,7 +61,7 @@ function spawn(tag, file, args, options, callback) { if (code || signal) debug(tag + ' code: %s, signal: %s', code, signal); if (code === 0) return callback(null); - var e = new BoxError(BoxError.SPAWN_ERROR, `${tag} exited with error ${code} signal ${signal}`); + let e = new BoxError(BoxError.SPAWN_ERROR, `${tag} exited with code ${code} signal ${signal}`); e.code = code; e.signal = signal; callback(e); @@ -69,7 +69,8 @@ function spawn(tag, file, args, options, callback) { cp.on('error', function (error) { debug(tag + ' code: %s, signal: %s', error.code, error.signal); - callback(error); + let e = new BoxError(BoxError.SPAWN_ERROR, `${tag} errored with code ${error.code} message ${error.message}`); + callback(e); }); return cp;