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

@@ -15,25 +15,11 @@ let assert = require('assert'),
HttpSuccess = require('connect-lastmile').HttpSuccess,
tasks = require('../tasks.js');
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.BAD_FIELD:
return new HttpError(400, error);
case BoxError.DATABASE_ERROR:
default:
return new HttpError(500, error);
}
}
function stopTask(req, res, next) {
assert.strictEqual(typeof req.params.taskId, 'string');
tasks.stopTask(req.params.taskId, function (error) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204, {}));
});
@@ -43,7 +29,7 @@ function get(req, res, next) {
assert.strictEqual(typeof req.params.taskId, 'string');
tasks.get(req.params.taskId, function (error, task) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, tasks.removePrivateFields(task)));
});
@@ -59,7 +45,7 @@ function list(req, res, next) {
if (req.query.type && typeof req.query.type !== 'string') return next(new HttpError(400, 'type must be a string'));
tasks.listByTypePaged(req.query.type || null, page, perPage, function (error, result) {
if (error) return next(toHttpError(error));
if (error) return next(BoxError.toHttpError(error));
result = result.map(tasks.removePrivateFields);
@@ -80,7 +66,7 @@ function getLogs(req, res, next) {
};
tasks.getLogs(req.params.taskId, 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',
@@ -110,7 +96,7 @@ function getLogStream(req, res, next) {
};
tasks.getLogs(req.params.taskId, 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',