diff --git a/src/eventlog.js b/src/eventlog.js index 4745cfd61..7ff800231 100644 --- a/src/eventlog.js +++ b/src/eventlog.js @@ -1,8 +1,6 @@ 'use strict'; exports = module.exports = { - EventLogError: EventLogError, - add: add, get: get, getAllPaged: getAllPaged, @@ -63,6 +61,7 @@ exports = module.exports = { }; var assert = require('assert'), + BoxError = require('./boxerror.js'), DatabaseError = require('./databaseerror.js'), debug = require('debug')('box:eventlog'), eventlogdb = require('./eventlogdb.js'), @@ -72,28 +71,6 @@ var assert = require('assert'), var NOOP_CALLBACK = function (error) { if (error) debug(error); }; -function EventLogError(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(EventLogError, Error); -EventLogError.INTERNAL_ERROR = 'Internal error'; -EventLogError.NOT_FOUND = 'Not Found'; - function add(action, source, data, callback) { assert.strictEqual(typeof action, 'string'); assert.strictEqual(typeof source, 'object'); @@ -105,10 +82,10 @@ function add(action, source, data, callback) { // we do only daily upserts for login actions, so they don't spam the db var api = action === exports.ACTION_USER_LOGIN ? eventlogdb.upsert : eventlogdb.add; api(uuid.v4(), action, source, data, function (error, id) { - if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error)); + if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error)); notifications.onEvent(id, action, source, data, function (error) { - if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error)); + if (error) return callback(error); callback(null, { id: id }); }); @@ -120,8 +97,8 @@ function get(id, callback) { assert.strictEqual(typeof callback, 'function'); eventlogdb.get(id, function (error, result) { - if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new EventLogError(EventLogError.NOT_FOUND, 'No such event')); - if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error)); + if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new BoxError(BoxError.NOT_FOUND, 'No such event')); + if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error)); callback(null, result); }); @@ -135,7 +112,7 @@ function getAllPaged(actions, search, page, perPage, callback) { assert.strictEqual(typeof callback, 'function'); eventlogdb.getAllPaged(actions, search, page, perPage, function (error, events) { - if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error)); + if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error)); callback(null, events); }); @@ -146,7 +123,7 @@ function getByCreationTime(creationTime, callback) { assert.strictEqual(typeof callback, 'function'); eventlogdb.getByCreationTime(creationTime, function (error, events) { - if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error)); + if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error)); callback(null, events); }); @@ -159,7 +136,7 @@ function cleanup(callback) { d.setDate(d.getDate() - 10); // 10 days ago eventlogdb.delByCreationTime(d, function (error) { - if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error)); + if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error)); callback(null); }); diff --git a/src/routes/eventlog.js b/src/routes/eventlog.js index 38c6957d6..9032ebf07 100644 --- a/src/routes/eventlog.js +++ b/src/routes/eventlog.js @@ -5,15 +5,24 @@ exports = module.exports = { list: list }; -var eventlog = require('../eventlog.js'), - EventLogError = eventlog.EventLogError, +var BoxError = require('../boxerror.js'), + eventlog = require('../eventlog.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); + case BoxError.DATABASE_ERROR: + default: + return new HttpError(500, error); + } +} + function get(req, res, next) { eventlog.get(req.params.eventId, function (error, result) { - if (error && error.reason === EventLogError.NOT_FOUND) return next(new HttpError(404, 'no such eventlog')); - if (error) return next(new HttpError(500, error)); + if (error) return next(toHttpError(error)); next(new HttpSuccess(200, { event: result })); }); @@ -34,7 +43,7 @@ function list(req, res, next) { if (req.query.action) actions.push(req.query.action); eventlog.getAllPaged(actions, req.query.search || null, page, perPage, function (error, result) { - if (error) return next(new HttpError(500, error)); + if (error) return next(toHttpError(error)); next(new HttpSuccess(200, { eventlogs: result })); }); diff --git a/src/test/eventlog-test.js b/src/test/eventlog-test.js index 22906dd08..bca6904a9 100644 --- a/src/test/eventlog-test.js +++ b/src/test/eventlog-test.js @@ -7,9 +7,9 @@ 'use strict'; var async = require('async'), + BoxError = require('../boxerror.js'), database = require('../database.js'), eventlog = require('../eventlog.js'), - EventLogError = eventlog.EventLogError, expect = require('expect.js'); function setup(done) { @@ -60,8 +60,8 @@ describe('Eventlog', function () { it('get of unknown id fails', function (done) { eventlog.get('notfoundid', function (error, result) { - expect(error).to.be.a(EventLogError); - expect(error.reason).to.be(EventLogError.NOT_FOUND); + expect(error).to.be.a(BoxError); + expect(error.reason).to.be(BoxError.NOT_FOUND); expect(result).to.not.be.ok(); done();