-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathExceptionHandlerAdvice.java
More file actions
50 lines (42 loc) · 1.74 KB
/
ExceptionHandlerAdvice.java
File metadata and controls
50 lines (42 loc) · 1.74 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
package com.bravo.user.controller.advice;
import com.bravo.user.exception.PaymentNotFoundException;
import com.bravo.user.model.dto.ErrorDto;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
@Slf4j
public class ExceptionHandlerAdvice {
@ExceptionHandler(value = BindException.class)
@ResponseBody
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public ErrorDto handleBindException(final BindException exception){
final Set<String> errors = exception
.getAllErrors().stream()
.filter(e -> e.getCodes() != null && e.getCodes().length > 1)
.map(e -> e.getCodes()[1])
.collect(Collectors.toSet());
final ErrorDto response = new ErrorDto();
response.setErrors(errors);
response.setException(exception.getClass());
response.setMessage("BadRequest: try to resolve the 'errors' in your request");
response.setStatusCode(400);
return response;
}
@ExceptionHandler(value = PaymentNotFoundException.class)
@ResponseBody
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public ErrorDto handlePaymentNotFoundException(final PaymentNotFoundException exception){
log.info("Payments not found, details: {}", exception.getMessage());
var errorDto = new ErrorDto();
errorDto.setMessage(exception.getMessage());
errorDto.setStatusCode(HttpStatus.NOT_FOUND.value());
return errorDto;
}
}