Move NotificationsError to BoxError

This commit is contained in:
Girish Ramakrishnan
2019-10-22 12:59:26 -07:00
parent 812f5cce99
commit 88818a1ec2
3 changed files with 33 additions and 49 deletions
+15 -7
View File
@@ -8,17 +8,26 @@ exports = module.exports = {
};
let assert = require('assert'),
BoxError = require('../boxerror.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
notifications = require('../notifications.js'),
NotificationsError = notifications.NotificationsError;
notifications = require('../notifications.js');
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 verifyOwnership(req, res, next) {
if (!req.params.notificationId) return next(); // skip for listing
notifications.get(req.params.notificationId, function (error, result) {
if (error && error.reason === NotificationsError.NOT_FOUND) return next(new HttpError(404, 'No such notification'));
if (error) return next(new HttpError(500, error));
if (error) return next(toHttpError(error));
if (result.userId !== req.user.id) return next(new HttpError(403, 'User is not owner'));
@@ -46,7 +55,7 @@ function list(req, res, next) {
else if (req.query.acknowledged) acknowledged = req.query.acknowledged === 'true' ? true : false;
notifications.getAllPaged(req.user.id, acknowledged, page, perPage, function (error, result) {
if (error) return next(new HttpError(500, error));
if (error) return next(toHttpError(error));
next(new HttpSuccess(200, { notifications: result }));
});
@@ -56,8 +65,7 @@ function ack(req, res, next) {
assert.strictEqual(typeof req.params.notificationId, 'string');
notifications.ack(req.params.notificationId, function (error) {
if (error && error.reason === NotificationsError.NOT_FOUND) return next(new HttpError(404, 'No such notification'));
if (error) return next(new HttpError(500, error));
if (error) return next(toHttpError(error));
next(new HttpSuccess(204, {}));
});