-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatisticsExceptionHandler.java
More file actions
30 lines (24 loc) · 1.29 KB
/
StatisticsExceptionHandler.java
File metadata and controls
30 lines (24 loc) · 1.29 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
28
29
30
package lv.ctco.springboottemplate.features.statistics;
import lv.ctco.springboottemplate.features.statistics.models.StatisticsErrorResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.List;
@RestControllerAdvice(assignableTypes = StatisticsController.class)
public class StatisticsExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(StatisticsExceptionHandler.class);
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<StatisticsErrorResponse> handleIllegalArgument(IllegalArgumentException ex) {
return ResponseEntity.badRequest()
.body(new StatisticsErrorResponse(List.of(ex.getMessage())));
}
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<StatisticsErrorResponse> handleRuntime(RuntimeException ex) {
log.error("Unhandled runtime exception in StatisticsController", ex);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new StatisticsErrorResponse(List.of("Internal server error")));
}
}