DefaultExceptionHandler.java
3.47 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
package com.diligrp.cashier.shared.exception;
import com.diligrp.cashier.shared.ErrorCode;
import com.diligrp.cashier.shared.domain.Message;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@RestControllerAdvice
public class DefaultExceptionHandler {
private static final Logger LOG = LoggerFactory.getLogger(DefaultExceptionHandler.class);
@ExceptionHandler(PlatformServiceException.class)
public Message<?> platformServiceException(PlatformServiceException ex) {
LOG.warn("cashier platform service exception", ex);
return Message.failure(ex.getCode(), ex.getMessage());
}
@ExceptionHandler(IllegalArgumentException.class)
public Message<?> illegalArgumentException(IllegalArgumentException ex) {
LOG.warn("cashier platform service exception", ex);
return Message.failure(ErrorCode.ILLEGAL_ARGUMENT_ERROR, ex.getMessage());
}
@ExceptionHandler(value = ConstraintViolationException.class)
public Message<?> constraintViolationException(ConstraintViolationException exs) {
Set<ConstraintViolation<?>> violations = exs.getConstraintViolations();
String msg = violations.stream().map(ConstraintViolation::getMessage)
.collect(Collectors.joining(","));
return Message.failure(ErrorCode.ILLEGAL_ARGUMENT_ERROR, msg);
}
@ExceptionHandler(value = {MethodArgumentNotValidException.class, BindException.class})
public Message<?> methodArgumentNotValidException(BindException ex) {
BindingResult bindingResult = ex.getBindingResult();
List<ObjectError> allErrors = bindingResult.getAllErrors();
String msg = allErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.joining(","));
return Message.failure(ErrorCode.ILLEGAL_ARGUMENT_ERROR, msg);
}
@ExceptionHandler(MissingServletRequestParameterException.class)
public Message<?> missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException ex) {
return Message.failure(ErrorCode.ILLEGAL_ARGUMENT_ERROR, String.format("缺少参数: %s", ex.getParameterName()));
}
@ExceptionHandler({HttpRequestMethodNotSupportedException.class})
public Message<?> handleRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
return Message.failure(ErrorCode.METHOD_NOT_SUPPORTED_ERROR, "请求方式错误!");
}
@ExceptionHandler(Exception.class)
public Message<?> defaultExceptionHandler(Exception ex) {
LOG.warn("cashier platform service exception", ex);
return Message.failure(ErrorCode.SYSTEM_UNKNOWN_ERROR, ErrorCode.MESSAGE_UNKNOWN_ERROR);
}
}