/
niki02
/
Practice_SBER
Обзор
Документация
Войти
/
niki02
/
Practice_SBER
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/com/example/creditcalculator/api/GlobalExceptionHandler.java
39 строк
2 KB
nikgilae
final commit
23 июн 2026, 12:08
23 июн 2026, 12:08
9a5fbe1
Код
Авторство
О чём код?
package com.example.creditcalculator.api; import com.example.creditcalculator.domain.InvalidPaymentException; import com.example.creditcalculator.domain.LoanClosedException; import com.example.creditcalculator.domain.LoanNotFoundException; import org.springframework.http.HttpStatus; import org.springframework.http.ProblemDetail; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import java.util.stream.Collectors; @RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(LoanNotFoundException.class) public ProblemDetail handleNotFound(LoanNotFoundException ex) { return ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, ex.getMessage()); } @ExceptionHandler(LoanClosedException.class) public ProblemDetail handleClosed(LoanClosedException ex) { return ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT, ex.getMessage()); } @ExceptionHandler(InvalidPaymentException.class) public ProblemDetail handleInvalidPayment(InvalidPaymentException ex) { return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ex.getMessage()); } @ExceptionHandler(MethodArgumentNotValidException.class) public ProblemDetail handleValidation(MethodArgumentNotValidException ex) { String detail = ex.getBindingResult().getFieldErrors().stream() .map(e -> e.getField() + ": " + e.getDefaultMessage()) .collect(Collectors.joining("; ")); return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, detail); } }