Skip to content
Snippets Groups Projects

Draft: Add HeadTA Job Offers + Constraint Violation Errors

Files
22
/*
* TAM
* Copyright (C) 2021 - Delft University of Technology
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package nl.tudelft.tam.controller.advice;
import java.util.stream.Collectors;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import nl.tudelft.tam.exception.ConstraintViolationDetailedException;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ConstraintViolationDetailedException.class)
public String handleConstraintViolationException(Model model, ConstraintViolationDetailedException ex) {
StackTraceElement topOfStack = ex.getStackTrace()[0];
String className = topOfStack.getClassName();
String methodName = topOfStack.getMethodName();
String errorMessage = ex.getConstraintViolations().stream()
.map(violation -> violation.getPropertyPath() + " " + violation.getMessage())
.collect(Collectors.joining(", "));
switch (className) {
case "nl.tudelft.tam.controller.JobOfferController":
switch (methodName) {
case "createJobOffer":
model.addAttribute("jobOfferCreationError", errorMessage);
return "job_offer/manage";
case "patchJobOffer":
model.addAttribute("editingError", errorMessage);
return "redirect:/job-offer/" + ex.getRedirectId();
case "patchJobOfferMessages":
model.addAttribute("messageEditingError", errorMessage);
return "redirect:/job-offer/" + ex.getRedirectId();
}
case "nl.tudelft.tam.controller.ExtraWorkController":
switch (methodName) {
case "createExtraWork":
model.addAttribute("extraWorkCreationError", errorMessage);
return "job_offer/manage";
case "patchExtraWork":
model.addAttribute("editingError", errorMessage);
return "redirect:/extra-work/" + ex.getRedirectId();
}
}
throw ex;
}
}
Loading