Refactor toHttpError code into BoxError
This commit is contained in:
@@ -16,20 +16,11 @@ var addons = require('../addons.js'),
|
||||
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);
|
||||
default:
|
||||
return new HttpError(500, error);
|
||||
}
|
||||
}
|
||||
|
||||
function getAll(req, res, next) {
|
||||
req.clearTimeout(); // can take a while to get status of all services
|
||||
|
||||
addons.getServices(function (error, result) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { services: result }));
|
||||
});
|
||||
@@ -39,7 +30,7 @@ function get(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.service, 'string');
|
||||
|
||||
addons.getService(req.params.service, function (error, result) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { service: result }));
|
||||
});
|
||||
@@ -56,7 +47,7 @@ function configure(req, res, next) {
|
||||
};
|
||||
|
||||
addons.configureService(req.params.service, data, function (error) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(202, {}));
|
||||
});
|
||||
@@ -77,7 +68,7 @@ function getLogs(req, res, next) {
|
||||
};
|
||||
|
||||
addons.getServiceLogs(req.params.service, options, function (error, logStream) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/x-logs',
|
||||
@@ -109,7 +100,7 @@ function getLogStream(req, res, next) {
|
||||
};
|
||||
|
||||
addons.getServiceLogs(req.params.service, options, function (error, logStream) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
@@ -135,7 +126,7 @@ function restart(req, res, next) {
|
||||
debug(`Restarting service ${req.params.service}`);
|
||||
|
||||
addons.restartService(req.params.service, function (error) {
|
||||
if (error) return next(toHttpError(error));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(202, {}));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user