forked from OnServerInit/OnServerInit-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestExceptionResponseHandler.java
More file actions
27 lines (21 loc) · 1.02 KB
/
RestExceptionResponseHandler.java
File metadata and controls
27 lines (21 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.imjustdoom.pluginsite.config.exception;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestControllerAdvice
@RequiredArgsConstructor
public class RestExceptionResponseHandler extends ResponseEntityExceptionHandler {
private final ObjectMapper mapper;
@ExceptionHandler(RestException.class)
public void handle(HttpServletResponse response, RestException exception) throws IOException {
if (!response.isCommitted()) {
response.setStatus(exception.getErrorCode().getHttpStatus().value());
this.mapper.writeValue(response.getWriter(), exception);
}
}
// Could add an error ticketing system for unknown errors (5xx) thrown
}