1
zj
2024-06-13 a4662cc65a02f258062bf6cc392ceb1017db9292
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许,不可做商业用途!
 *
 * 版权所有,侵权必究!
 */
 
package com.yami.trading.common.config;
 
import com.yami.trading.common.domain.Result;
import com.yami.trading.common.exception.BusinessException;
import com.yami.trading.common.exception.YamiShopBindException;
import com.yami.trading.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
 
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 自定义错误处理器
 *
 * @author LGH
 */
@Slf4j
@Controller
@RestControllerAdvice
public class DefaultExceptionHandlerConfig {
 
 
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<String> exception(MethodArgumentNotValidException e) {
        log.error("ExceptionHandler MethodArgumentNotValid:", e);
        BindingResult bindingResult = e.getBindingResult();
        List<ObjectError> allErrors = bindingResult.getAllErrors();
        List<ErrorMsg> errorMsgs = new ArrayList<>();
 
        allErrors.forEach(objectError -> {
            ErrorMsg errorMsg = new ErrorMsg();
            FieldError fieldError = (FieldError) objectError;
            errorMsg.setField(fieldError.getField());
            errorMsg.setMessage(fieldError.getDefaultMessage());
            errorMsgs.add(errorMsg);
        });
        return Result.failed(errorMsgs.toString());
    }
 
 
    @ExceptionHandler(value = ConstraintViolationException.class)
    public Result handleValidationException(ConstraintViolationException e) {
        log.error("ExceptionHandler ConstraintViolationException:", e);
        StringBuffer errorBuffer = new StringBuffer();
        for (ConstraintViolation<?> s : e.getConstraintViolations()) {
            errorBuffer.append(s.getMessage() + ";");
        }
        return Result.failed(errorBuffer.toString());
    }
 
    @ExceptionHandler(BindException.class)
    @ResponseStatus(HttpStatus.OK)
    public Result<String> validExceptionHandler(BindException e) {
        log.error("ExceptionHandler BindException:", e);
        StringBuilder message = new StringBuilder();
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        for (FieldError error : fieldErrors) {
            message.append(error.getField()).append(error.getDefaultMessage()).append(",");
        }
        message = new StringBuilder(message.substring(0, message.length() - 1));
        return Result.failed(message.toString());
 
    }
 
 
    @ExceptionHandler(value = RuntimeException.class)
    public Result<String> handlerRuntimeException(HttpServletRequest req,
                                                  RuntimeException ex) {
        log.error("RuntimeException Message :", ex);
        String message = ex.getMessage();
        if (StringUtils.isNotEmpty(message)) {
            return Result.failed(message);
        } else {
            return Result.failed(ex.toString());
        }
    }
 
 
    @ExceptionHandler(YamiShopBindException.class)
    public Result unauthorizedExceptionHandler(YamiShopBindException e) {
        log.error("YamiException Message :", e);
        return Result.of(null, e.getCode(), e.getMessage());
    }
 
    @ExceptionHandler(BusinessException.class)
    public Result businessExceptionHandler(BusinessException e) {
        log.error("BusinessException Message :", e);
        return Result.failed(e.getMessage());
    }
}