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

@@ -45,28 +45,11 @@ var assert = require('assert'),
var mailProxy = middleware.proxy(url.parse('http://127.0.0.1:2020'));
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.INTERNAL_ERROR:
default:
return new HttpError(500, error);
}
}
function getDomain(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
mail.getDomain(req.params.domain, function (error, result) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, mail.removePrivateFields(result)));
});
@@ -78,7 +61,7 @@ function addDomain(req, res, next) {
if (typeof req.body.domain !== 'string') return next(new HttpError(400, 'domain must be a string'));
mail.addDomain(req.body.domain, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, { domain: req.body.domain }));
});
@@ -106,7 +89,7 @@ function setDnsRecords(req, res, next) {
req.clearTimeout();
mail.setDnsRecords(req.params.domain, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201));
});
@@ -116,7 +99,7 @@ function removeDomain(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
mail.removeDomain(req.params.domain, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
});
@@ -129,7 +112,7 @@ function getStatus(req, res, next) {
req.clearTimeout();
mail.getStatus(req.params.domain, function (error, records) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, records));
});
@@ -142,7 +125,7 @@ function setMailFromValidation(req, res, next) {
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled is required'));
mail.setMailFromValidation(req.params.domain, req.body.enabled, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
@@ -160,7 +143,7 @@ function setCatchAllAddress(req, res, next) {
}
mail.setCatchAllAddress(req.params.domain, req.body.addresses, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
@@ -178,7 +161,7 @@ function setMailRelay(req, res, next) {
if ('acceptSelfSignedCerts' in req.body && typeof req.body.acceptSelfSignedCerts !== 'boolean') return next(new HttpError(400, 'acceptSelfSignedCerts must be a boolean'));
mail.setMailRelay(req.params.domain, req.body, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
@@ -191,7 +174,7 @@ function setMailEnabled(req, res, next) {
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled is required'));
mail.setMailEnabled(req.params.domain, !!req.body.enabled, auditSource.fromRequest(req), function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
@@ -204,7 +187,7 @@ function sendTestMail(req, res, next) {
if (!req.body.to || typeof req.body.to !== 'string') return next(new HttpError(400, 'to must be a non-empty string'));
mail.sendTestMail(req.params.domain, req.body.to, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
@@ -220,7 +203,7 @@ function listMailboxes(req, res, next) {
if (!perPage || perPage < 0) return next(new HttpError(400, 'per_page query param has to be a postive number'));
mail.listMailboxes(req.params.domain, page, perPage, function (error, result) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { mailboxes: result }));
});
@@ -231,7 +214,7 @@ function getMailbox(req, res, next) {
assert.strictEqual(typeof req.params.name, 'string');
mail.getMailbox(req.params.name, req.params.domain, function (error, result) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { mailbox: result }));
});
@@ -244,7 +227,7 @@ function addMailbox(req, res, next) {
if (typeof req.body.userId !== 'string') return next(new HttpError(400, 'userId must be a string'));
mail.addMailbox(req.body.name, req.params.domain, req.body.userId, auditSource.fromRequest(req), function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, {}));
});
@@ -257,7 +240,7 @@ function updateMailbox(req, res, next) {
if (typeof req.body.userId !== 'string') return next(new HttpError(400, 'userId must be a string'));
mail.updateMailboxOwner(req.params.name, req.params.domain, req.body.userId, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
});
@@ -268,7 +251,7 @@ function removeMailbox(req, res, next) {
assert.strictEqual(typeof req.params.name, 'string');
mail.removeMailbox(req.params.name, req.params.domain, auditSource.fromRequest(req), function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, {}));
});
@@ -284,7 +267,7 @@ function listAliases(req, res, next) {
if (!perPage || perPage < 0) return next(new HttpError(400, 'per_page query param has to be a postive number'));
mail.listAliases(req.params.domain, page, perPage, function (error, result) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { aliases: result }));
});
@@ -295,7 +278,7 @@ function getAliases(req, res, next) {
assert.strictEqual(typeof req.params.name, 'string');
mail.getAliases(req.params.name, req.params.domain, function (error, result) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { aliases: result }));
});
@@ -313,7 +296,7 @@ function setAliases(req, res, next) {
}
mail.setAliases(req.params.name, req.params.domain, req.body.aliases, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
@@ -323,7 +306,7 @@ function getLists(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
mail.getLists(req.params.domain, function (error, result) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { lists: result }));
});
@@ -334,7 +317,7 @@ function getList(req, res, next) {
assert.strictEqual(typeof req.params.name, 'string');
mail.getList(req.params.domain, req.params.name, function (error, result) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { list: result }));
});
@@ -353,7 +336,7 @@ function addList(req, res, next) {
}
mail.addList(req.body.name, req.params.domain, req.body.members, auditSource.fromRequest(req), function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, {}));
});
@@ -371,7 +354,7 @@ function updateList(req, res, next) {
}
mail.updateList(req.params.name, req.params.domain, req.body.members, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
});
@@ -382,7 +365,7 @@ function removeList(req, res, next) {
assert.strictEqual(typeof req.params.name, 'string');
mail.removeList(req.params.name, req.params.domain, auditSource.fromRequest(req), function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
});