fix(updateMyCurrentChallenge): Bad mongo id will return user error

Mark these errors to be reported to the user instead of logged as a
server fault
This commit is contained in:
Berkeley Martinez
2018-01-22 17:08:33 -08:00
parent b2e58ab212
commit ffbf3bc826
4 changed files with 43 additions and 30 deletions

View File

@@ -1,6 +1,9 @@
import { isMongoId } from 'validator';
import { check } from 'express-validator/check';
import { ifNoUser401 } from '../utils/middleware';
import {
ifNoUser401,
createValidatorErrorHandler
} from '../utils/middleware';
import supportedLanguages from '../../common/utils/supported-languages.js';
export default function settingsController(app) {
@@ -51,11 +54,14 @@ export default function settingsController(app) {
);
}
const updateMyCurrentChallengeValidators = [
check('currentChallengeId')
.isMongoId()
.withMessage('currentChallengeId is not a valid challenge ID')
];
function updateMyCurrentChallenge(req, res, next) {
const { user, body: { currentChallengeId } } = req;
if (!isMongoId('' + currentChallengeId)) {
return next(new Error(`${currentChallengeId} is not a valid ObjectId`));
}
return user.update$({ currentChallengeId }).subscribe(
() => res.json({
message:
@@ -65,6 +71,14 @@ export default function settingsController(app) {
);
}
api.post(
'/update-my-current-challenge',
ifNoUser401,
updateMyCurrentChallengeValidators,
createValidatorErrorHandler('errors'),
updateMyCurrentChallenge
);
function updateMyTheme(req, res, next) {
req.checkBody('theme', 'Theme is invalid.').isLength({ min: 4 });
const { body: { theme } } = req;
@@ -117,13 +131,6 @@ export default function settingsController(app) {
ifNoUser401,
updateMyLang
);
api.post(
'/update-my-current-challenge',
ifNoUser401,
updateMyCurrentChallenge
);
api.post(
'/update-my-theme',
ifNoUser401,