Move DomainsError to BoxError
This commit is contained in:
+24
-20
@@ -14,17 +14,32 @@ exports = module.exports = {
|
||||
|
||||
var assert = require('assert'),
|
||||
auditSource = require('../auditsource.js'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
domains = require('../domains.js'),
|
||||
DomainsError = domains.DomainsError,
|
||||
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 && error.reason === DomainsError.NOT_FOUND) return next(new HttpError(404, 'No such domain'));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
if (domain.locked) return next(new HttpError(423, 'This domain is locked'));
|
||||
|
||||
@@ -68,10 +83,7 @@ function add(req, res, next) {
|
||||
};
|
||||
|
||||
domains.add(req.body.domain, data, auditSource.fromRequest(req), function (error) {
|
||||
if (error && error.reason === DomainsError.ALREADY_EXISTS) return next(new HttpError(409, error.message));
|
||||
if (error && error.reason === DomainsError.BAD_FIELD) return next(new HttpError(400, error.message));
|
||||
if (error && error.reason === DomainsError.INVALID_PROVIDER) return next(new HttpError(400, error.message));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(201, { domain: req.body.domain, config: req.body.config }));
|
||||
});
|
||||
@@ -81,8 +93,7 @@ function get(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.domain, 'string');
|
||||
|
||||
domains.get(req.params.domain, function (error, result) {
|
||||
if (error && error.reason === DomainsError.NOT_FOUND) return next(new HttpError(404, error.message));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, domains.removePrivateFields(result)));
|
||||
});
|
||||
@@ -134,10 +145,7 @@ function update(req, res, next) {
|
||||
};
|
||||
|
||||
domains.update(req.params.domain, data, auditSource.fromRequest(req), function (error) {
|
||||
if (error && error.reason === DomainsError.NOT_FOUND) return next(new HttpError(404, error.message));
|
||||
if (error && error.reason === DomainsError.BAD_FIELD) return next(new HttpError(400, error.message));
|
||||
if (error && error.reason === DomainsError.INVALID_PROVIDER) return next(new HttpError(400, error.message));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(204, {}));
|
||||
});
|
||||
@@ -147,9 +155,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 && error.reason === DomainsError.NOT_FOUND) return next(new HttpError(404, error.message));
|
||||
if (error && error.reason === DomainsError.IN_USE) return next(new HttpError(409, 'Domain is still in use. Remove all apps and mailboxes using this domain'));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(204));
|
||||
});
|
||||
@@ -164,10 +170,8 @@ function checkDnsRecords(req, res, next) {
|
||||
req.clearTimeout();
|
||||
|
||||
domains.checkDnsRecords(req.query.subdomain, req.params.domain, function (error, result) {
|
||||
if (error && error.reason === DomainsError.NOT_FOUND) return next(new HttpError(404, error.message)); // domain (and not record!) not found
|
||||
if (error && error.reason === DomainsError.EXTERNAL_ERROR) return next(new HttpError(424, error.message));
|
||||
if (error && error.reason === DomainsError.ACCESS_DENIED) return next(new HttpSuccess(200, { error: { reason: error.reason, message: error.message }}));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
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));
|
||||
|
||||
next(new HttpSuccess(200, { needsOverwrite: result.needsOverwrite }));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user