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
+5 -23
View File
@@ -17,24 +17,6 @@ var assert = require('assert'),
users = require('../users.js'),
_ = require('underscore');
function toHttpError(error) {
switch (error.reason) {
case BoxError.NOT_FOUND:
return new HttpError(404, error);
case BoxError.ALREADY_EXISTS:
return new HttpError(409, error);
case BoxError.BAD_FIELD:
return new HttpError(400, error);
case BoxError.EXTERNAL_ERROR:
return new HttpError(424, error);
case BoxError.INVALID_CREDENTIALS:
return new HttpError(412, error);
case BoxError.INTERNAL_ERROR:
default:
return new HttpError(500, error);
}
}
function get(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
@@ -61,7 +43,7 @@ function update(req, res, next) {
var data = _.pick(req.body, 'email', 'fallbackEmail', 'displayName');
users.update(req.user.id, data, auditSource.fromRequest(req), function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
});
@@ -74,7 +56,7 @@ function changePassword(req, res, next) {
if (typeof req.body.newPassword !== 'string') return next(new HttpError(400, 'newPassword must be a string'));
users.setPassword(req.user.id, req.body.newPassword, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
});
@@ -84,7 +66,7 @@ function setTwoFactorAuthenticationSecret(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
users.setTwoFactorAuthenticationSecret(req.user.id, function (error, result) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, { secret: result.secret, qrcode: result.qrcode }));
});
@@ -97,7 +79,7 @@ function enableTwoFactorAuthentication(req, res, next) {
if (!req.body.totpToken || typeof req.body.totpToken !== 'string') return next(new HttpError(400, 'totpToken must be a nonempty string'));
users.enableTwoFactorAuthentication(req.user.id, req.body.totpToken, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, {}));
});
@@ -107,7 +89,7 @@ function disableTwoFactorAuthentication(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
users.disableTwoFactorAuthentication(req.user.id, function (error) {
if (error) return next(new HttpError(500, error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, {}));
});