Refactor toHttpError code into BoxError
This commit is contained in:
@@ -20,25 +20,6 @@ var assert = require('assert'),
|
||||
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
||||
users = require('../users.js');
|
||||
|
||||
function toHttpError(error) {
|
||||
switch (error.reason) {
|
||||
case BoxError.NOT_FOUND:
|
||||
return new HttpError(404, error);
|
||||
case BoxError.ALREADY_EXISTS:
|
||||
case BoxError.CONFLICT:
|
||||
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 create(req, res, next) {
|
||||
assert.strictEqual(typeof req.body, 'object');
|
||||
|
||||
@@ -54,7 +35,7 @@ function create(req, res, next) {
|
||||
var displayName = req.body.displayName || '';
|
||||
|
||||
users.create(username, password, email, displayName, { invitor: req.user, admin: req.body.admin }, auditSource.fromRequest(req), function (error, user) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
var userInfo = {
|
||||
id: user.id,
|
||||
@@ -89,7 +70,7 @@ function update(req, res, next) {
|
||||
if ('active' in req.body && typeof req.body.active !== 'boolean') return next(new HttpError(400, 'active must be a boolean'));
|
||||
|
||||
users.update(req.params.userId, req.body, auditSource.fromRequest(req), function (error) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(204));
|
||||
});
|
||||
@@ -105,7 +86,7 @@ function list(req, res, next) {
|
||||
if (req.query.search && typeof req.query.search !== 'string') return next(new HttpError(400, 'search must be a string'));
|
||||
|
||||
users.getAllPaged(req.query.search || null, page, perPage, function (error, results) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
results = results.map(users.removeRestrictedFields);
|
||||
|
||||
@@ -118,7 +99,7 @@ function get(req, res, next) {
|
||||
assert.strictEqual(typeof req.user, 'object');
|
||||
|
||||
users.get(req.params.userId, function (error, result) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, users.removePrivateFields(result)));
|
||||
});
|
||||
@@ -130,7 +111,7 @@ function remove(req, res, next) {
|
||||
if (req.user.id === req.params.userId) return next(new HttpError(409, 'Not allowed to remove yourself.'));
|
||||
|
||||
users.remove(req.params.userId, auditSource.fromRequest(req), function (error) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(204));
|
||||
});
|
||||
@@ -144,7 +125,7 @@ function verifyPassword(req, res, next) {
|
||||
if (typeof req.body.password !== 'string') return next(new HttpError(400, 'API call requires user password'));
|
||||
|
||||
users.verifyWithUsername(req.user.username, req.body.password, function (error) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
req.body.password = '<redacted>'; // this will prevent logs from displaying plain text password
|
||||
|
||||
@@ -156,7 +137,7 @@ function createInvite(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.userId, 'string');
|
||||
|
||||
users.createInvite(req.params.userId, function (error, resetToken) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { resetToken: resetToken }));
|
||||
});
|
||||
@@ -166,7 +147,7 @@ function sendInvite(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.userId, 'string');
|
||||
|
||||
users.sendInvite(req.params.userId, { invitor: req.user }, function (error) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { }));
|
||||
});
|
||||
@@ -179,7 +160,7 @@ function setGroups(req, res, next) {
|
||||
if (!Array.isArray(req.body.groupIds)) return next(new HttpError(400, 'API call requires a groups array.'));
|
||||
|
||||
users.setMembership(req.params.userId, req.body.groupIds, function (error) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(204));
|
||||
});
|
||||
@@ -192,7 +173,7 @@ function changePassword(req, res, next) {
|
||||
if (typeof req.body.password !== 'string') return next(new HttpError(400, 'password must be a string'));
|
||||
|
||||
users.setPassword(req.params.userId, req.body.password, function (error) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(204));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user