feat(backend): Eigen error handler toegevoegd.

Hiervoor was ook refactoring aan de exception-klassen nodig.
This commit is contained in:
Gerald Schmittinger 2025-03-30 12:25:41 +02:00
parent bc94b25a6a
commit aaa71aa648
14 changed files with 103 additions and 55 deletions

View file

@ -5,6 +5,9 @@ import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js
import { mapToStudent, mapToStudentDTO, StudentDTO } from '../interfaces/student.js';
import { mapToSubmissionDTO, mapToSubmissionDTOId, SubmissionDTO, SubmissionDTOId } from '../interfaces/submission.js';
import { getAllAssignments } from './assignments.js';
import {UniqueConstraintViolationException} from "@mikro-orm/core";
import {ConflictException} from "../exceptions/conflict-exception";
export async function getAllStudents(full: boolean): Promise<StudentDTO[] | string[]> {
const studentRepository = getStudentRepository();
@ -29,11 +32,12 @@ export async function createStudent(userData: StudentDTO): Promise<StudentDTO |
try {
const newStudent = mapToStudent(userData);
await studentRepository.save(newStudent);
return mapToStudentDTO(newStudent);
} catch (e) {
console.log(e);
return null;
} catch (e: unknown) {
if (e instanceof UniqueConstraintViolationException) {
throw new ConflictException(`There is already a user with username '${userData.username}'.`);
}
throw e;
}
}

View file

@ -2,17 +2,16 @@ import {
getClassRepository,
getLearningObjectRepository,
getQuestionRepository,
getStudentRepository,
getTeacherRepository,
} from '../data/repositories.js';
import { Teacher } from '../entities/users/teacher.entity.js';
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
import { getClassStudents } from './classes.js';
import { StudentDTO } from '../interfaces/student.js';
import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question.js';
import { mapToUser } from '../interfaces/user.js';
import { mapToTeacher, mapToTeacherDTO, TeacherDTO } from '../interfaces/teacher.js';
import { teachersOnly } from '../middleware/auth/auth.js';
import {UniqueConstraintViolationException} from "@mikro-orm/core";
import {ConflictException} from "../exceptions/conflict-exception";
export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> {
const teacherRepository = getTeacherRepository();
@ -40,8 +39,10 @@ export async function createTeacher(userData: TeacherDTO): Promise<TeacherDTO |
return mapToTeacherDTO(newTeacher);
} catch (e) {
console.log(e);
return null;
if (e instanceof UniqueConstraintViolationException) {
throw new ConflictException(`There is already a user with username '${userData.username}'.`);
}
throw e;
}
}