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
+6 -22
View File
@@ -19,27 +19,11 @@ var assert = require('assert'),
HttpError = require('connect-lastmile').HttpError,
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.EXTERNAL_ERROR: //
return new HttpError(424, error);
case BoxError.DATABASE_ERROR:
default:
return new HttpError(500, error);
}
}
function verifyDomainLock(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
domains.get(req.params.domain, function (error, domain) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
if (domain.locked) return next(new HttpError(423, 'This domain is locked'));
@@ -83,7 +67,7 @@ function add(req, res, next) {
};
domains.add(req.body.domain, data, auditSource.fromRequest(req), function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, { domain: req.body.domain, config: req.body.config }));
});
@@ -93,7 +77,7 @@ function get(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
domains.get(req.params.domain, function (error, result) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, domains.removePrivateFields(result)));
});
@@ -145,7 +129,7 @@ function update(req, res, next) {
};
domains.update(req.params.domain, data, auditSource.fromRequest(req), function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204, {}));
});
@@ -155,7 +139,7 @@ function del(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
domains.del(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));
});
@@ -171,7 +155,7 @@ function checkDnsRecords(req, res, next) {
domains.checkDnsRecords(req.query.subdomain, req.params.domain, function (error, result) {
if (error && error.reason === BoxError.ACCESS_DENIED) return next(new HttpSuccess(200, { error: { reason: error.reason, message: error.message }}));
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { needsOverwrite: result.needsOverwrite }));
});