Move BackupsError to BoxError

This commit is contained in:
Girish Ramakrishnan
2019-10-22 20:36:20 -07:00
parent df142994a8
commit 8c9ce30d29
9 changed files with 132 additions and 145 deletions

View File

@@ -22,7 +22,7 @@ exports = module.exports = {
var assert = require('assert'),
async = require('async'),
backups = require('../backups.js'),
BackupsError = require('../backups.js').BackupsError,
BoxError = require('../boxerror.js'),
debug = require('debug')('box:storage/gcs'),
EventEmitter = require('events'),
GCS = require('@google-cloud/storage').Storage,
@@ -68,7 +68,7 @@ function upload(apiConfig, backupFilePath, sourceStream, callback) {
function done(error) {
if (error) {
debug('[%s] upload: gcp upload error.', backupFilePath, error);
return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, `Error uploading ${backupFilePath}. Message: ${error.message} HTTP Code: ${error.code}`));
return callback(new BoxError(BoxError.EXTERNAL_ERROR, `Error uploading ${backupFilePath}. Message: ${error.message} HTTP Code: ${error.code}`));
}
callback(null);
@@ -95,10 +95,10 @@ function download(apiConfig, backupFilePath, callback) {
var readStream = file.createReadStream()
.on('error', function(error) {
if (error && error.code == 404){
ps.emit('error', new BackupsError(BackupsError.NOT_FOUND));
ps.emit('error', new BoxError(BoxError.NOT_FOUND));
} else {
debug('[%s] download: gcp stream error.', backupFilePath, error);
ps.emit('error', new BackupsError(BackupsError.EXTERNAL_ERROR, error));
ps.emit('error', new BoxError(BoxError.EXTERNAL_ERROR, error));
}
})
;
@@ -151,8 +151,8 @@ function copy(apiConfig, oldFilePath, newFilePath) {
getBucket(apiConfig).file(entry.fullPath).copy(path.join(newFilePath, relativePath), function(error) {
if (error) debug('copyBackup: gcs copy error', error);
if (error && error.code === 404) return iteratorCallback(new BackupsError(BackupsError.NOT_FOUND, 'Old backup not found'));
if (error) return iteratorCallback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
if (error && error.code === 404) return iteratorCallback(new BoxError(BoxError.NOT_FOUND, 'Old backup not found'));
if (error) return iteratorCallback(new BoxError(BoxError.EXTERNAL_ERROR, error.message));
iteratorCallback(null);
});
@@ -219,13 +219,13 @@ function testConfig(apiConfig, callback) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof callback, 'function');
if (typeof apiConfig.projectId !== 'string') return callback(new BackupsError(BackupsError.BAD_FIELD, 'projectId must be a string'));
if (!apiConfig.credentials || typeof apiConfig.credentials !== 'object') return callback(new BackupsError(BackupsError.BAD_FIELD, 'credentials must be an object'));
if (typeof apiConfig.credentials.client_email !== 'string') return callback(new BackupsError(BackupsError.BAD_FIELD, 'credentials.client_email must be a string'));
if (typeof apiConfig.credentials.private_key !== 'string') return callback(new BackupsError(BackupsError.BAD_FIELD, 'credentials.private_key must be a string'));
if (typeof apiConfig.projectId !== 'string') return callback(new BoxError(BoxError.BAD_FIELD, 'projectId must be a string'));
if (!apiConfig.credentials || typeof apiConfig.credentials !== 'object') return callback(new BoxError(BoxError.BAD_FIELD, 'credentials must be an object'));
if (typeof apiConfig.credentials.client_email !== 'string') return callback(new BoxError(BoxError.BAD_FIELD, 'credentials.client_email must be a string'));
if (typeof apiConfig.credentials.private_key !== 'string') return callback(new BoxError(BoxError.BAD_FIELD, 'credentials.private_key must be a string'));
if (typeof apiConfig.bucket !== 'string') return callback(new BackupsError(BackupsError.BAD_FIELD, 'bucket must be a string'));
if (typeof apiConfig.prefix !== 'string') return callback(new BackupsError(BackupsError.BAD_FIELD, 'prefix must be a string'));
if (typeof apiConfig.bucket !== 'string') return callback(new BoxError(BoxError.BAD_FIELD, 'bucket must be a string'));
if (typeof apiConfig.prefix !== 'string') return callback(new BoxError(BoxError.BAD_FIELD, 'prefix must be a string'));
// attempt to upload and delete a file with new credentials
var bucket = getBucket(apiConfig);
@@ -239,16 +239,16 @@ function testConfig(apiConfig, callback) {
uploadStream.on('error', function(error) {
debug('testConfig: failed uploading cloudron-testfile', error);
if (error && error.code && (error.code == 403 || error.code == 404)) {
return callback(new BackupsError(BackupsError.BAD_FIELD, error.message));
return callback(new BoxError(BoxError.BAD_FIELD, error.message));
}
return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
return callback(new BoxError(BoxError.EXTERNAL_ERROR, error.message));
});
uploadStream.on('finish', function() {
debug('testConfig: uploaded cloudron-testfile ' + JSON.stringify(arguments));
bucket.file(path.join(apiConfig.prefix, 'cloudron-testfile')).delete(function(error) {
if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
if (error) return callback(new BoxError(BoxError.EXTERNAL_ERROR, error.message));
debug('testConfig: deleted cloudron-testfile');
callback();
});