Move GroupsError to BoxError
This commit is contained in:
+22
-14
@@ -10,10 +10,24 @@ exports = module.exports = {
|
||||
};
|
||||
|
||||
var assert = require('assert'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
groups = require('../groups.js'),
|
||||
HttpError = require('connect-lastmile').HttpError,
|
||||
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
||||
GroupsError = groups.GroupsError;
|
||||
HttpSuccess = require('connect-lastmile').HttpSuccess;
|
||||
|
||||
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.INTERNAL_ERROR:
|
||||
default:
|
||||
return new HttpError(500, error);
|
||||
}
|
||||
}
|
||||
|
||||
function create(req, res, next) {
|
||||
assert.strictEqual(typeof req.body, 'object');
|
||||
@@ -21,9 +35,7 @@ function create(req, res, next) {
|
||||
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name must be string'));
|
||||
|
||||
groups.create(req.body.name, function (error, group) {
|
||||
if (error && error.reason === GroupsError.BAD_FIELD) return next(new HttpError(400, error.message));
|
||||
if (error && error.reason === GroupsError.ALREADY_EXISTS) return next(new HttpError(409, 'Already exists'));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
var groupInfo = {
|
||||
id: group.id,
|
||||
@@ -38,8 +50,7 @@ function get(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.groupId, 'string');
|
||||
|
||||
groups.getWithMembers(req.params.groupId, function (error, result) {
|
||||
if (error && error.reason === GroupsError.NOT_FOUND) return next(new HttpError(404, 'No such group'));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, result));
|
||||
});
|
||||
@@ -52,8 +63,7 @@ function update(req, res, next) {
|
||||
if ('name' in req.body && typeof req.body.name !== 'string') return next(new HttpError(400, 'name must be a string'));
|
||||
|
||||
groups.update(req.params.groupId, req.body, function (error) {
|
||||
if (error && error.reason === GroupsError.BAD_FIELD) return next(new HttpError(400, error.message));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { }));
|
||||
});
|
||||
@@ -66,8 +76,7 @@ function updateMembers(req, res, next) {
|
||||
if (!Array.isArray(req.body.userIds)) return next(new HttpError(404, 'userIds must be an array'));
|
||||
|
||||
groups.setMembers(req.params.groupId, req.body.userIds, function (error) {
|
||||
if (error && error.reason === GroupsError.NOT_FOUND) return next(new HttpError(404, 'Invalid group or user id'));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { }));
|
||||
});
|
||||
@@ -75,7 +84,7 @@ function updateMembers(req, res, next) {
|
||||
|
||||
function list(req, res, next) {
|
||||
groups.getAll(function (error, result) {
|
||||
if (error) return next(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { groups: result }));
|
||||
});
|
||||
@@ -85,8 +94,7 @@ function remove(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.groupId, 'string');
|
||||
|
||||
groups.remove(req.params.groupId, function (error) {
|
||||
if (error && error.reason === GroupsError.NOT_FOUND) return next(new HttpError(404, 'Group not found'));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(204));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user