Move TaskError into BoxError
This commit is contained in:
49
src/tasks.js
49
src/tasks.js
@@ -16,8 +16,6 @@ exports = module.exports = {
|
||||
|
||||
removePrivateFields: removePrivateFields,
|
||||
|
||||
TaskError: TaskError,
|
||||
|
||||
// task types. if you add a task here, fill up the function table in taskworker
|
||||
TASK_APP: 'app',
|
||||
TASK_BACKUP: 'backup',
|
||||
@@ -41,6 +39,7 @@ exports = module.exports = {
|
||||
|
||||
let assert = require('assert'),
|
||||
async = require('async'),
|
||||
BoxError = require('./boxerror.js'),
|
||||
child_process = require('child_process'),
|
||||
DatabaseError = require('./databaseerror.js'),
|
||||
debug = require('debug')('box:tasks'),
|
||||
@@ -49,36 +48,12 @@ let assert = require('assert'),
|
||||
spawn = require('child_process').spawn,
|
||||
split = require('split'),
|
||||
taskdb = require('./taskdb.js'),
|
||||
util = require('util'),
|
||||
_ = require('underscore');
|
||||
|
||||
let gTasks = {}; // indexed by task id
|
||||
|
||||
const NOOP_CALLBACK = function (error) { if (error) debug(error); };
|
||||
|
||||
function TaskError(reason, errorOrMessage) {
|
||||
assert.strictEqual(typeof reason, 'string');
|
||||
assert(errorOrMessage instanceof Error || typeof errorOrMessage === 'string' || typeof errorOrMessage === 'undefined');
|
||||
|
||||
Error.call(this);
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
|
||||
this.name = this.constructor.name;
|
||||
this.reason = reason;
|
||||
if (typeof errorOrMessage === 'undefined') {
|
||||
this.message = reason;
|
||||
} else if (typeof errorOrMessage === 'string') {
|
||||
this.message = errorOrMessage;
|
||||
} else {
|
||||
this.message = 'Internal error';
|
||||
this.nestedError = errorOrMessage;
|
||||
}
|
||||
}
|
||||
util.inherits(TaskError, Error);
|
||||
TaskError.INTERNAL_ERROR = 'Internal Error';
|
||||
TaskError.BAD_STATE = 'Bad State';
|
||||
TaskError.NOT_FOUND = 'Not Found';
|
||||
|
||||
function postProcess(result) {
|
||||
assert.strictEqual(typeof result, 'object');
|
||||
|
||||
@@ -97,8 +72,8 @@ function get(id, callback) {
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
taskdb.get(id, function (error, task) {
|
||||
if (error && error.reason == DatabaseError.NOT_FOUND) return callback(new TaskError(TaskError.NOT_FOUND));
|
||||
if (error) return callback(new TaskError(TaskError.INTERNAL_ERROR, error));
|
||||
if (error && error.reason == DatabaseError.NOT_FOUND) return callback(new BoxError(BoxError.NOT_FOUND));
|
||||
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
|
||||
|
||||
postProcess(task);
|
||||
|
||||
@@ -114,8 +89,8 @@ function update(id, task, callback) {
|
||||
debug(`${id}: ${JSON.stringify(task)}`);
|
||||
|
||||
taskdb.update(id, task, function (error) {
|
||||
if (error && error.reason == DatabaseError.NOT_FOUND) return callback(new TaskError(TaskError.NOT_FOUND));
|
||||
if (error) return callback(new TaskError(TaskError.INTERNAL_ERROR, error));
|
||||
if (error && error.reason == DatabaseError.NOT_FOUND) return callback(new BoxError(BoxError.NOT_FOUND));
|
||||
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
|
||||
|
||||
callback();
|
||||
});
|
||||
@@ -137,11 +112,11 @@ function setCompletedByType(type, task, callback) {
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
listByTypePaged(type, 1, 1, function (error, results) {
|
||||
if (error) return callback(new TaskError(TaskError.INTERNAL_ERROR, error));
|
||||
if (results.length !== 1) return callback(new TaskError(TaskError.NOT_FOUND));
|
||||
if (error) return callback(error);
|
||||
if (results.length !== 1) return callback(new BoxError(BoxError.NOT_FOUND));
|
||||
|
||||
setCompleted(results[0].id, task, function (error) {
|
||||
if (error) return callback(new TaskError(TaskError.INTERNAL_ERROR, error));
|
||||
if (error) return callback(error);
|
||||
|
||||
callback();
|
||||
});
|
||||
@@ -154,7 +129,7 @@ function add(type, args, callback) {
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
taskdb.add({ type: type, percent: 0, message: 'Starting ...', args: args }, function (error, taskId) {
|
||||
if (error) return callback(new TaskError(TaskError.INTERNAL_ERROR, error));
|
||||
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
|
||||
|
||||
callback(null, taskId);
|
||||
});
|
||||
@@ -169,7 +144,7 @@ function startTask(taskId, options, callback) {
|
||||
let fd = safe.fs.openSync(logFile, 'a'); // will autoclose. append is for apptask logs
|
||||
if (!fd) {
|
||||
debug(`startTask: unable to get log filedescriptor ${safe.error.message}`);
|
||||
return callback(new TaskError(TaskError.INTERNAL_ERROR, safe.error));
|
||||
return callback(new BoxError(BoxError.FS_ERROR, safe.error));
|
||||
}
|
||||
|
||||
debug(`startTask - starting task ${taskId}. logs at ${logFile}`);
|
||||
@@ -218,7 +193,7 @@ function stopTask(id, callback) {
|
||||
assert.strictEqual(typeof id, 'string');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
if (!gTasks[id]) return callback(new TaskError(TaskError.BAD_STATE, 'task is not active'));
|
||||
if (!gTasks[id]) return callback(new BoxError(BoxError.BAD_STATE, 'task is not active'));
|
||||
|
||||
debug(`stopTask: stopping task ${id}`);
|
||||
|
||||
@@ -242,7 +217,7 @@ function listByTypePaged(type, page, perPage, callback) {
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
taskdb.listByTypePaged(type, page, perPage, function (error, tasks) {
|
||||
if (error) return callback(new TaskError(TaskError.INTERNAL_ERROR, error));
|
||||
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
|
||||
|
||||
tasks.forEach(postProcess);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user