Move CloudronError to BoxError

This commit is contained in:
Girish Ramakrishnan
2019-10-22 14:06:19 -07:00
parent 9842b6d4a1
commit a7614cef2e
2 changed files with 38 additions and 58 deletions

View File

@@ -19,8 +19,8 @@ exports = module.exports = {
let assert = require('assert'),
async = require('async'),
auditSource = require('../auditsource.js'),
BoxError = require('../boxerror.js'),
cloudron = require('../cloudron.js'),
CloudronError = cloudron.CloudronError,
custom = require('../custom.js'),
disks = require('../disks.js'),
externalldap = require('../externalldap.js'),
@@ -30,6 +30,19 @@ let assert = require('assert'),
updateChecker = require('../updatechecker.js'),
UpdaterError = require('../updater.js').UpdaterError;
function toHttpError(error) {
switch (error.reason) {
case BoxError.NOT_FOUND:
return new HttpError(404, error);
case BoxError.BAD_STATE:
return new HttpError(409, error);
case BoxError.DATABASE_ERROR:
case BoxError.INTERNAL_ERROR:
default:
return new HttpError(500, error);
}
}
function reboot(req, res, next) {
// Finish the request, to let the appstore know we triggered the reboot
next(new HttpSuccess(202, {}));
@@ -39,7 +52,7 @@ function reboot(req, res, next) {
function isRebootRequired(req, res, next) {
cloudron.isRebootRequired(function (error, result) {
if (error) return next(new HttpError(500, error));
if (error) return next(toHttpError(error));
next(new HttpSuccess(200, { rebootRequired: result }));
});
@@ -47,7 +60,7 @@ function isRebootRequired(req, res, next) {
function getConfig(req, res, next) {
cloudron.getConfig(function (error, cloudronConfig) {
if (error) return next(new HttpError(500, error));
if (error) return next(toHttpError(error));
next(new HttpSuccess(200, cloudronConfig));
});
@@ -55,7 +68,7 @@ function getConfig(req, res, next) {
function getDisks(req, res, next) {
disks.getDisks(function (error, result) {
if (error) return next(new HttpError(500, error));
if (error) return next(toHttpError(error));
next(new HttpSuccess(200, result));
});
@@ -103,8 +116,7 @@ function getLogs(req, res, next) {
};
cloudron.getLogs(req.params.unit, options, function (error, logStream) {
if (error && error.reason === CloudronError.BAD_FIELD) return next(new HttpError(404, 'Invalid type'));
if (error) return next(new HttpError(500, error));
if (error) return next(toHttpError(error));
res.writeHead(200, {
'Content-Type': 'application/x-logs',
@@ -133,8 +145,7 @@ function getLogStream(req, res, next) {
};
cloudron.getLogs(req.params.unit, options, function (error, logStream) {
if (error && error.reason === CloudronError.BAD_FIELD) return next(new HttpError(404, 'Invalid type'));
if (error) return next(new HttpError(500, error));
if (error) return next(toHttpError(error));
res.writeHead(200, {
'Content-Type': 'text/event-stream',
@@ -160,8 +171,7 @@ function setDashboardAndMailDomain(req, res, next) {
if (!custom.spec().domains.changeDashboardDomain) return next(new HttpError(405, 'feature disabled by admin'));
cloudron.setDashboardAndMailDomain(req.body.domain, auditSource.fromRequest(req), function (error) {
if (error && error.reason === CloudronError.BAD_FIELD) return next(new HttpError(404, error.message));
if (error) return next(new HttpError(500, error));
if (error) return next(toHttpError(error));
next(new HttpSuccess(204, {}));
});
@@ -173,9 +183,7 @@ function prepareDashboardDomain(req, res, next) {
if (!custom.spec().domains.changeDashboardDomain) return next(new HttpError(405, 'feature disabled by admin'));
cloudron.prepareDashboardDomain(req.body.domain, auditSource.fromRequest(req), function (error, taskId) {
if (error && error.reason === CloudronError.BAD_FIELD) return next(new HttpError(404, error.message));
if (error && error.reason === CloudronError.BAD_STATE) return next(new HttpError(409, error.message));
if (error) return next(new HttpError(500, error));
if (error) return next(toHttpError(error));
next(new HttpSuccess(202, { taskId }));
});
@@ -183,8 +191,7 @@ function prepareDashboardDomain(req, res, next) {
function renewCerts(req, res, next) {
cloudron.renewCerts({ domain: req.body.domain || null }, auditSource.fromRequest(req), function (error, taskId) {
if (error && error.reason === CloudronError.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(202, { taskId }));
});