Commit f2d367d2 authored by Christopher Fullarton's avatar Christopher Fullarton

Changes some exceptions to return the http request successfully but with an...

Changes some exceptions to return the http request successfully but with an error message in the body
parent 08abf029
import { DeleteWriteOpResultObject } from 'mongodb';
import {
BadRequestError, BodyParam, Delete, Get, HeaderParam, InternalServerError, JsonController, Param, Post, Put, UnauthorizedError,
} from 'routing-controllers';
import { BadRequestError, BodyParam, Delete, Get, HeaderParam, JsonController, Param, Post, Put, UnauthorizedError } from 'routing-controllers';
import MemberDAO from '../../db/MemberDAO';
import QuizDAO from '../../db/quiz/QuizDAO';
import { MemberEntity } from '../../entities/member/MemberEntity';
......@@ -47,15 +45,15 @@ export class MemberRouter extends AbstractRouter {
const activeQuiz: IQuizEntity = QuizDAO.getActiveQuizByName(member.currentQuizName);
if (!activeQuiz) {
throw new InternalServerError(JSON.stringify({
return {
status: StatusProtocol.Failed,
step: MessageProtocol.IsInactive,
payload: {},
}));
};
}
if (!member.name || (activeQuiz.sessionConfig.nicks.restrictToCasLogin && !member.ticket)) {
throw new InternalServerError(JSON.stringify({
throw new BadRequestError(JSON.stringify({
status: StatusProtocol.Failed,
step: MessageProtocol.InvalidParameters,
payload: {},
......@@ -86,7 +84,7 @@ export class MemberRouter extends AbstractRouter {
} catch (ex) {
LoggerService.error('Cannot add member', ex.message);
throw new InternalServerError(JSON.stringify({
throw new BadRequestError(JSON.stringify({
status: StatusProtocol.Failed,
step: MessageProtocol.Added,
payload: { message: ex.message },
......@@ -198,11 +196,13 @@ export class MemberRouter extends AbstractRouter {
const activeQuiz: IQuizEntity = QuizDAO.getActiveQuizByName(quizName);
if (!activeQuiz) {
throw new InternalServerError(JSON.stringify({
status: StatusProtocol.Failed,
step: MessageProtocol.IsInactive,
payload: {},
}));
return {
status: StatusProtocol.Success,
step: MessageProtocol.GetPlayers,
payload: {
members: [],
},
};
}
return {
......@@ -220,11 +220,11 @@ export class MemberRouter extends AbstractRouter {
const activeQuiz: IQuizEntity = QuizDAO.getActiveQuizByName(quizName);
if (!activeQuiz) {
throw new InternalServerError(JSON.stringify({
status: StatusProtocol.Failed,
step: MessageProtocol.IsInactive,
payload: {},
}));
return {
status: StatusProtocol.Success,
step: MessageProtocol.GetRemainingNicks,
payload: { nicknames: [] },
};
}
const names: Array<string> = activeQuiz.sessionConfig.nicks.selectedNicks.filter((nick) => {
return !MemberDAO.getMembersOfQuiz(activeQuiz.name).find(member => member.name === nick);
......
......@@ -64,8 +64,6 @@ export class QuizRouter extends AbstractRouter {
const payload: IQuizStatusPayload = {};
if (quiz) {
const members = MemberDAO.getMembersOfQuiz(quiz.name);
if ([QuizState.Active, QuizState.Running].includes(quiz.state)) {
payload.provideNickSelection = quiz.sessionConfig.nicks.selectedNicks.length > 0;
payload.authorizeViaCas = quiz.sessionConfig.nicks.restrictToCasLogin;
......@@ -217,8 +215,11 @@ export class QuizRouter extends AbstractRouter {
): Promise<object> {
const quiz = QuizDAO.getQuizByName(quizName);
if (!quiz || ![QuizState.Active, QuizState.Running].includes(quiz.state)) {
console.error('No quiz found or not active', JSON.stringify(quiz));
throw new InternalServerError(MessageProtocol.IsInactive);
return {
status: StatusProtocol.Failed,
step: MessageProtocol.IsInactive,
payload: {},
};
}
if (quiz.privateKey !== token) {
......@@ -307,11 +308,7 @@ export class QuizRouter extends AbstractRouter {
const activeQuiz: IQuizEntity = QuizDAO.getActiveQuizByName(quizName);
if (!activeQuiz) {
throw new InternalServerError(JSON.stringify({
status: StatusProtocol.Failed,
step: MessageProtocol.IsInactive,
payload: {},
}));
return;
}
DbDAO.updateOne(DbCollection.Quizzes, { _id: QuizDAO.getQuizByName(quizName).id }, { currentStartTimestamp: -1 });
......@@ -348,11 +345,11 @@ export class QuizRouter extends AbstractRouter {
const activeQuiz: IQuizEntity = QuizDAO.getActiveQuizByName(quizName);
if (!activeQuiz) {
throw new InternalServerError(JSON.stringify({
return {
status: StatusProtocol.Failed,
step: MessageProtocol.IsInactive,
payload: {},
}));
};
}
const index = activeQuiz.currentQuestionIndex < 0 ? 0 : activeQuiz.currentQuestionIndex;
return {
......@@ -373,11 +370,11 @@ export class QuizRouter extends AbstractRouter {
const activeQuiz: IQuizEntity = QuizDAO.getActiveQuizByName(quizName);
if (!activeQuiz) {
throw new InternalServerError(JSON.stringify({
return {
status: StatusProtocol.Failed,
step: MessageProtocol.IsInactive,
step: MessageProtocol.ReadingConfirmationRequested,
payload: {},
}));
};
}
activeQuiz.nextQuestion();
activeQuiz.requestReadingConfirmation();
......@@ -394,11 +391,11 @@ export class QuizRouter extends AbstractRouter {
const activeQuiz: IQuizEntity = QuizDAO.getActiveQuizByName(quizName);
if (!activeQuiz) {
throw new InternalServerError(JSON.stringify({
return {
status: StatusProtocol.Failed,
step: MessageProtocol.IsInactive,
step: MessageProtocol.GetStartTime,
payload: {},
}));
};
}
return {
status: StatusProtocol.Success,
......@@ -413,11 +410,11 @@ export class QuizRouter extends AbstractRouter {
const activeQuiz: IQuizEntity = QuizDAO.getQuizByName(quizName);
if (!activeQuiz) {
throw new InternalServerError(JSON.stringify({
return {
status: StatusProtocol.Failed,
step: MessageProtocol.Unavailable,
step: MessageProtocol.UpdatedSettings,
payload: {},
}));
};
}
return {
......@@ -436,10 +433,14 @@ export class QuizRouter extends AbstractRouter {
const activeQuiz: IQuizEntity = QuizDAO.getQuizByName(quizName);
if (!activeQuiz) {
throw new InternalServerError(MessageProtocol.IsInactive);
return {
status: StatusProtocol.Failed,
step: MessageProtocol.UpdatedSettings,
payload: {},
};
}
if (activeQuiz.privateKey !== token) {
throw new InternalServerError(MessageProtocol.InsufficientPermissions);
throw new UnauthorizedError(MessageProtocol.InsufficientPermissions);
}
DbDAO.updateOne(DbCollection.Quizzes, { _id: activeQuiz.id }, { ['sessionConfig.' + quizSettings.target]: quizSettings.state });
......@@ -462,7 +463,7 @@ export class QuizRouter extends AbstractRouter {
}
const activeQuizzesAmount = QuizDAO.getActiveQuizzes();
if (activeQuizzesAmount.length >= settings.public.limitActiveQuizzes) {
throw new InternalServerError(MessageProtocol.TooMuchActiveQuizzes);
throw new BadRequestError(MessageProtocol.TooMuchActiveQuizzes);
}
if (settings.public.createQuizPasswordRequired) {
if (!serverPassword) {
......@@ -661,13 +662,8 @@ export class QuizRouter extends AbstractRouter {
): Promise<Buffer> {
const activeQuiz: IQuizEntity = QuizDAO.getActiveQuizByName(quizName);
if (!activeQuiz) {
throw new InternalServerError(JSON.stringify({
status: StatusProtocol.Failed,
step: MessageProtocol.IsInactive,
payload: {},
}));
return;
}
// TODO: The quiz contains the rewritten cached asset urls. Restore them to the original value!
......@@ -700,11 +696,11 @@ export class QuizRouter extends AbstractRouter {
const activeQuiz: IQuizEntity = QuizDAO.getActiveQuizByName(quizName);
if (!activeQuiz) {
throw new InternalServerError(JSON.stringify({
return {
status: StatusProtocol.Failed,
step: MessageProtocol.IsInactive,
step: MessageProtocol.GetLeaderboardData,
payload: {},
}));
};
}
const member = MemberDAO.getMemberByToken(authorization);
......
import * as jwt from 'jsonwebtoken';
import { BadRequestError } from 'routing-controllers';
import { UnauthorizedError } from 'routing-controllers';
import UserDAO from '../db/UserDAO';
import { MessageProtocol, StatusProtocol } from '../enums/Message';
import { UserRole } from '../enums/UserRole';
......@@ -11,14 +11,14 @@ export class AuthService {
public static authenticate({ username, password, searchRoles }: { username: any; password: any; searchRoles: UserRole[] }): boolean {
const user = UserDAO.getUser(username);
if (!user || !UserDAO.validateUser(username, password)) {
throw new BadRequestError(JSON.stringify({
throw new UnauthorizedError(JSON.stringify({
status: StatusProtocol.Failed,
message: MessageProtocol.NotAuthorized,
}));
}
const token = UserDAO.getUser(username).token;
if (!token || !UserDAO.validateTokenForUser(username, token)) {
throw new BadRequestError(JSON.stringify({
throw new UnauthorizedError(JSON.stringify({
status: StatusProtocol.Failed,
message: MessageProtocol.NotAuthorized,
}));
......@@ -26,7 +26,7 @@ export class AuthService {
const hasRoles = (searchRoles as unknown as Array<UserRole>).some(role => user.userAuthorizations.includes(role));
if (!hasRoles) {
throw new BadRequestError(`Only user with ${searchRoles.join(' &')} roles in the ldap user can pass.`);
throw new UnauthorizedError(`Only user with ${searchRoles.join(' &')} roles in the ldap user can pass.`);
}
return true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment