forked from yandex-praktikum/java-shareit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorHandler.java
More file actions
41 lines (34 loc) · 1.41 KB
/
ErrorHandler.java
File metadata and controls
41 lines (34 loc) · 1.41 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
31
32
33
34
35
36
37
38
39
40
41
package ru.practicum.shareit.exception;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@Slf4j
@RestControllerAdvice
public class ErrorHandler {
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Map<String, String> catchAllExceptions(Exception ex) {
log.error("500: ", ex);
return Map.of("error", "Произошла ошибка");
}
@ExceptionHandler({ValidationException.class, MethodArgumentNotValidException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map<String, String> catchValidationErrors(Exception ex) {
log.error("400: {}", ex.getMessage());
return Map.of("error", "Ошибка валидации");
}
@ExceptionHandler(ConflictException.class)
@ResponseStatus(HttpStatus.CONFLICT)
public Map<String, String> catchConflictStatus(ConflictException ex) {
log.error("409: {}", ex.getMessage());
return Map.of("error", ex.getMessage());
}
@ExceptionHandler(NotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public Map<String, String> catchNotFoundStatus(NotFoundException ex) {
log.error("404: {}", ex.getMessage());
return Map.of("error", ex.getMessage());
}
}