Move TaskError into BoxError
This commit is contained in:
@@ -10,18 +10,30 @@ exports = module.exports = {
|
||||
};
|
||||
|
||||
let assert = require('assert'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
HttpError = require('connect-lastmile').HttpError,
|
||||
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
||||
TaskError = require('../tasks.js').TaskError,
|
||||
tasks = require('../tasks.js');
|
||||
|
||||
function toHttpError(appError) {
|
||||
switch (appError.reason) {
|
||||
case BoxError.NOT_FOUND:
|
||||
return new HttpError(404, appError);
|
||||
case BoxError.BAD_STATE:
|
||||
return new HttpError(409, appError);
|
||||
case BoxError.BAD_FIELD:
|
||||
return new HttpError(400, appError);
|
||||
case BoxError.DATABASE_ERROR:
|
||||
default:
|
||||
return new HttpError(500, appError);
|
||||
}
|
||||
}
|
||||
|
||||
function stopTask(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.taskId, 'string');
|
||||
|
||||
tasks.stopTask(req.params.taskId, function (error) {
|
||||
if (error && error.reason === TaskError.NOT_FOUND) return next(new HttpError(404, 'No such task'));
|
||||
if (error && error.reason === TaskError.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(204, {}));
|
||||
});
|
||||
@@ -31,8 +43,7 @@ function get(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.taskId, 'string');
|
||||
|
||||
tasks.get(req.params.taskId, function (error, task) {
|
||||
if (error && error.reason === TaskError.NOT_FOUND) return next(new HttpError(404, 'No such task'));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, tasks.removePrivateFields(task)));
|
||||
});
|
||||
@@ -48,7 +59,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(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
result = result.map(tasks.removePrivateFields);
|
||||
|
||||
@@ -69,8 +80,7 @@ function getLogs(req, res, next) {
|
||||
};
|
||||
|
||||
tasks.getLogs(req.params.taskId, options, function (error, logStream) {
|
||||
if (error && error.reason === TaskError.NOT_FOUND) return next(new HttpError(404, 'No such task'));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/x-logs',
|
||||
@@ -100,8 +110,7 @@ function getLogStream(req, res, next) {
|
||||
};
|
||||
|
||||
tasks.getLogs(req.params.taskId, options, function (error, logStream) {
|
||||
if (error && error.reason === TaskError.NOT_FOUND) return next(new HttpError(404, 'No such task'));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
if (error) return next(toHttpError(error));
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
|
||||
Reference in New Issue
Block a user