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

@@ -17,24 +17,9 @@ var assert = require('assert'),
safe = require('safetydance'),
settings = require('../settings.js');
function toHttpError(appError) {
switch (appError.reason) {
case BoxError.NOT_FOUND:
return new HttpError(404, appError);
case BoxError.BAD_FIELD:
return new HttpError(400, appError);
case BoxError.EXTERNAL_ERROR:
return new HttpError(424, appError);
case BoxError.INTERNAL_ERROR:
case BoxError.DATABASE_ERROR:
default:
return new HttpError(500, appError);
}
}
function getAppAutoupdatePattern(req, res, next) {
settings.getAppAutoupdatePattern(function (error, pattern) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { pattern: pattern }));
});
@@ -46,7 +31,7 @@ function setAppAutoupdatePattern(req, res, next) {
if (typeof req.body.pattern !== 'string') return next(new HttpError(400, 'pattern is required'));
settings.setAppAutoupdatePattern(req.body.pattern, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
@@ -54,7 +39,7 @@ function setAppAutoupdatePattern(req, res, next) {
function getBoxAutoupdatePattern(req, res, next) {
settings.getBoxAutoupdatePattern(function (error, pattern) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { pattern: pattern }));
});
@@ -66,7 +51,7 @@ function setBoxAutoupdatePattern(req, res, next) {
if (typeof req.body.pattern !== 'string') return next(new HttpError(400, 'pattern is required'));
settings.setBoxAutoupdatePattern(req.body.pattern, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
@@ -78,7 +63,7 @@ function setCloudronName(req, res, next) {
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name is required'));
settings.setCloudronName(req.body.name, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, {}));
});
@@ -86,7 +71,7 @@ function setCloudronName(req, res, next) {
function getCloudronName(req, res, next) {
settings.getCloudronName(function (error, name) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { name: name }));
});
@@ -94,7 +79,7 @@ function getCloudronName(req, res, next) {
function getTimeZone(req, res, next) {
settings.getTimeZone(function (error, tz) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { timeZone: tz }));
});
@@ -106,7 +91,7 @@ function setTimeZone(req, res, next) {
if (typeof req.body.timeZone !== 'string') return next(new HttpError(400, 'timeZone is required'));
settings.setTimeZone(req.body.timeZone, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
@@ -119,7 +104,7 @@ function setCloudronAvatar(req, res, next) {
var avatar = safe.fs.readFileSync(req.files.avatar.path);
settings.setCloudronAvatar(avatar, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, {}));
});
@@ -127,7 +112,7 @@ function setCloudronAvatar(req, res, next) {
function getCloudronAvatar(req, res, next) {
settings.getCloudronAvatar(function (error, avatar) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
// avoid caching the avatar on the client to see avatar changes immediately
res.set('Cache-Control', 'no-cache');
@@ -139,7 +124,7 @@ function getCloudronAvatar(req, res, next) {
function getBackupConfig(req, res, next) {
settings.getBackupConfig(function (error, backupConfig) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
// always send provider as it is used by the UI to figure if backups are disabled ('noop' backend)
if (!custom.spec().backups.configurable) {
@@ -170,7 +155,7 @@ function setBackupConfig(req, res, next) {
req.clearTimeout();
settings.setBackupConfig(req.body, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
@@ -178,7 +163,7 @@ function setBackupConfig(req, res, next) {
function getPlatformConfig(req, res, next) {
settings.getPlatformConfig(function (error, config) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, config));
});
@@ -196,7 +181,7 @@ function setPlatformConfig(req, res, next) {
}
settings.setPlatformConfig(req.body, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
@@ -204,7 +189,7 @@ function setPlatformConfig(req, res, next) {
function getExternalLdapConfig(req, res, next) {
settings.getExternalLdapConfig(function (error, config) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, config));
});
@@ -221,7 +206,7 @@ function setExternalLdapConfig(req, res, next) {
if ('bindPassword' in req.body && typeof req.body.bindPassword !== 'string') return next(new HttpError(400, 'bindPassword must be a string'));
settings.setExternalLdapConfig(req.body, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
@@ -229,7 +214,7 @@ function setExternalLdapConfig(req, res, next) {
function getDynamicDnsConfig(req, res, next) {
settings.getDynamicDnsConfig(function (error, enabled) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { enabled: enabled }));
});
@@ -243,7 +228,7 @@ function setDynamicDnsConfig(req, res, next) {
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled boolean is required'));
settings.setDynamicDnsConfig(req.body.enabled, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
@@ -251,7 +236,7 @@ function setDynamicDnsConfig(req, res, next) {
function getUnstableAppsConfig(req, res, next) {
settings.getUnstableAppsConfig(function (error, enabled) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { enabled: enabled }));
});
@@ -263,7 +248,7 @@ function setUnstableAppsConfig(req, res, next) {
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled boolean is required'));
settings.setUnstableAppsConfig(req.body.enabled, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
@@ -271,7 +256,7 @@ function setUnstableAppsConfig(req, res, next) {
function getRegistryConfig(req, res, next) {
settings.getRegistryConfig(function (error, registryConfig) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, docker.removePrivateFields(registryConfig)));
});
@@ -286,7 +271,7 @@ function setRegistryConfig(req, res, next) {
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password is required'));
settings.setRegistryConfig(req.body, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200));
});