-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionController.java
More file actions
84 lines (77 loc) · 4.37 KB
/
Copy pathExceptionController.java
File metadata and controls
84 lines (77 loc) · 4.37 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package ru.netology.cardwork.controller;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import ru.netology.cardwork.dto.ErrorResponseDto;
import ru.netology.cardwork.exception.CardNotFoundException;
import ru.netology.cardwork.exception.TransferNotPossibleException;
import ru.netology.cardwork.exception.VerificationFailureException;
import javax.validation.ConstraintViolationException;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Handler of exceptional cases
*/
@ControllerAdvice
@NoArgsConstructor
@Slf4j
public class ExceptionController {
private static final AtomicInteger idCount = new AtomicInteger();
/**
* Handles situations when something is not ok with data in request:
* constrains on fields are broken or required fields missing,
* when any of cards involved is absent or data of donor card not valid,
* also when the received verification code is not correct.
* @param bad an exception being caught.
* @return a response entity with status code 400 and an ErrorResponseDto as body.
*/
@ExceptionHandler({MethodArgumentNotValidException.class,
HttpMessageNotReadableException.class,
CardNotFoundException.class,
VerificationFailureException.class,
ConstraintViolationException.class})
ResponseEntity<ErrorResponseDto> handleBadRequest(Exception bad) {
log.debug("Caught an exception: {}", bad.getClass());
String report = bad.getLocalizedMessage();
log.trace("Original message of exception: {}", report);
if (bad instanceof MethodArgumentNotValidException) { // ???
report = report.substring(report.lastIndexOf("[") + 1, report.lastIndexOf("]]"));
} else if (bad instanceof HttpMessageNotReadableException) {
report = "неправильная структура запроса";
}
log.warn("Transfer attempt rejected because of inappropriate request: {}", report);
return new ResponseEntity<>(new ErrorResponseDto("Ошибка в запросе: %s".formatted(report),
idCount.getAndIncrement()), HttpStatus.BAD_REQUEST);
}
/**
* Handles situations when the request is ok but the transfer is not possible for some reason:
* when any of cards involved is inactive or doesn't have a proper currency account, or when funds at donor account
* are insufficient.
* @param tnpe an exception being caught.
* @return a response entity with status code 500 and an ErrorResponseDto as body.
*/
@ExceptionHandler(TransferNotPossibleException.class)
ResponseEntity<ErrorResponseDto> handleTransferError(TransferNotPossibleException tnpe) {
log.debug("Caught an exception: {}", tnpe.getClass());
log.warn("Transfer attempt rejected because of error: {}", tnpe.getLocalizedMessage());
return new ResponseEntity<>(new ErrorResponseDto("Совершение перевода невозможно: %s".formatted(tnpe.getMessage()),
idCount.getAndIncrement()), HttpStatus.INTERNAL_SERVER_ERROR);
}
/**
* Handles all other exceptional cases that are likely during service exploitation.
* Particularly a situation when a verification code comes for the operation whose id is not at the wait list somehow.
* @param se an exception being caught.
* @return a response entity with status code 500 and an ErrorResponseDto as body.
*/
@ExceptionHandler(Exception.class)
ResponseEntity<ErrorResponseDto> handleServerError(Exception se) {
log.debug("Caught an exception: {}", se.getClass());
log.error("Some error at the server: {}", se.getLocalizedMessage());
return new ResponseEntity<>(new ErrorResponseDto("Что-то пошло не так: %s".formatted(se.getMessage()),
idCount.getAndIncrement()), HttpStatus.INTERNAL_SERVER_ERROR);
}
}