diff --git a/src/apps.js b/src/apps.js index 9d4edd387..b89833252 100644 --- a/src/apps.js +++ b/src/apps.js @@ -48,6 +48,7 @@ var addons = require('./addons.js'), assert = require('assert'), async = require('async'), backups = require('./backups.js'), + BackupsError = backups.BackupsError, certificates = require('./certificates.js'), config = require('./config.js'), constants = require('./constants.js'), @@ -678,7 +679,9 @@ function restore(appId, data, auditSource, callback) { var func = data.backupId ? backups.getRestoreConfig.bind(null, data.backupId) : function (next) { return next(null, { manifest: app.manifest }); }; func(function (error, restoreConfig) { + if (error && error.reason === BackupsError.EXTERNAL_ERROR) return callback(new AppsError(AppsError.EXTERNAL_ERROR, error.message)); if (error) return callback(new AppsError(AppsError.INTERNAL_ERROR, error)); + if (!restoreConfig) callback(new AppsError(AppsError.EXTERNAL_ERROR, 'Could not get restore config')); // re-validate because this new box version may not accept old configs diff --git a/src/backups.js b/src/backups.js index c6908bbf9..7d4ac4cfb 100644 --- a/src/backups.js +++ b/src/backups.js @@ -176,11 +176,14 @@ function getRestoreConfig(backupId, callback) { api(backupConfig.provider).getRestoreUrl(backupConfig, configFile, function (error, result) { if (error) return callback(error); - superagent.get(result.url, function (error, response) { + superagent.get(result.url).buffer(true).end(function (error, response) { if (error && !error.response) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message)); if (response.statusCode !== 200) return callback(new Error('Invalid response code when getting config.json : ' + response.statusCode)); - return callback(null, response.body); + var config = safe.JSON.parse(response.text); + if (!config) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, 'Error in config:' + safe.error.message)); + + return callback(null, config); }); }); }); diff --git a/src/routes/apps.js b/src/routes/apps.js index caa595d96..5ed8cde7b 100644 --- a/src/routes/apps.js +++ b/src/routes/apps.js @@ -185,6 +185,7 @@ function restoreApp(req, res, next) { if (error && error.reason === AppsError.NOT_FOUND) return next(new HttpError(404, 'No such app')); if (error && error.reason === AppsError.BAD_FIELD) return next(new HttpError(400, error.message)); if (error && error.reason === AppsError.BAD_STATE) return next(new HttpError(409, error.message)); + if (error && error.reason === AppsError.EXTERNAL_ERROR) return next(new HttpError(424, error.message)); if (error) return next(new HttpError(500, error)); next(new HttpSuccess(202, { }));