added spring boot ControllerAdvice
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package de.weinbrecht.luc.bpm.architecture.recommendation.adapter.in.web;
|
||||
|
||||
import io.github.domainprimitives.validation.InvariantException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
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;
|
||||
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
|
||||
@ControllerAdvice
|
||||
public class RestExceptionHandlerAdvice extends ResponseEntityExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = InvariantException.class)
|
||||
protected ResponseEntity<Object> handleInvariantConflict(InvariantException ex, WebRequest request) {
|
||||
return handleExceptionInternal(ex, ex.getMessage(), new HttpHeaders(), BAD_REQUEST, request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package de.weinbrecht.luc.bpm.architecture.recommendation.adapter.in.web;
|
||||
|
||||
import io.github.domainprimitives.validation.InvariantException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("error")
|
||||
public class DemoController {
|
||||
|
||||
@GetMapping()
|
||||
public void provideInvariant() {
|
||||
throw new InvariantException("Foo", "Testing");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package de.weinbrecht.luc.bpm.architecture.recommendation.adapter.in.web;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
|
||||
@WebMvcTest(DemoController.class)
|
||||
class RestExceptionHandlerAdviceTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
void should_handle_invariants() throws Exception {
|
||||
mockMvc.perform(get("/error")).andExpect(status().isBadRequest());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user