Better ilegalArgumentException handling

This commit is contained in:
Matthias Engelien
2024-09-16 08:09:50 +02:00
parent ebc66ff8f6
commit 0a7559b94f
2 changed files with 21 additions and 2 deletions

View File

@@ -0,0 +1,19 @@
package de.etecture.ga.exception;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice
public class ResponseExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = { IllegalArgumentException.class })
protected ResponseEntity<Object> handleConflict(RuntimeException ex, WebRequest request) {
String bodyOfResponse = ex.getMessage();
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_ACCEPTABLE, request);
}
}

View File

@@ -68,8 +68,8 @@ class GarageApiControllerTest {
this.mockMvc
.perform(
MockMvcRequestBuilders.get("/werkstatt/{werkstattId}/termin/{terminId}", testGarage.id(), "-5"))
.andDo(MockMvcResultHandlers.print()).andExpect(MockMvcResultMatchers.status().isNotFound());
MockMvcRequestBuilders.get("/werkstatt/{werkstattId}/termin/{terminId}", testGarage.id(), "9999a"))
.andDo(MockMvcResultHandlers.print()).andExpect(MockMvcResultMatchers.status().isNotAcceptable());
this.mockMvc
.perform(MockMvcRequestBuilders.get("/werkstatt/{werkstattId}/termin/{terminId}", 2,