Refactor toHttpError code into BoxError

This commit is contained in:
Girish Ramakrishnan
2019-10-24 18:05:14 -07:00
parent d6365ff27f
commit 6e57f8cc03
18 changed files with 180 additions and 396 deletions

View File

@@ -18,22 +18,6 @@ var assert = require('assert'),
sysinfo = require('../sysinfo.js'),
superagent = require('superagent');
function toHttpError(error) {
switch (error.reason) {
case BoxError.EXTERNAL_ERROR:
return new HttpError(424, error);
case BoxError.NOT_FOUND:
return new HttpError(404, error);
case BoxError.CONFLICT:
return new HttpError(409, error);
case BoxError.BAD_FIELD:
return new HttpError(400, error);
case BoxError.INTERNAL_ERROR:
default:
return new HttpError(500, error);
}
}
function providerTokenAuth(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
@@ -75,7 +59,7 @@ function setup(req, res, next) {
req.clearTimeout();
provision.setup(dnsConfig, req.body.backupConfig || null, auditSource.fromRequest(req), function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
@@ -98,7 +82,7 @@ function activate(req, res, next) {
debug('activate: username:%s ip:%s', username, ip);
provision.activate(username, password, email, displayName, ip, auditSource.fromRequest(req), function (error, info) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, info));
});
@@ -119,7 +103,7 @@ function restore(req, res, next) {
if (typeof req.body.version !== 'string') return next(new HttpError(400, 'version must be a string'));
provision.restore(backupConfig, req.body.backupId, req.body.version, auditSource.fromRequest(req), function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
@@ -127,7 +111,7 @@ function restore(req, res, next) {
function getStatus(req, res, next) {
provision.getStatus(function (error, status) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, status));
});