86183
2022-09-09 0d999e33085c0a25c5525242748f6aa62a401159
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
package com.dsh.app.advice;
 
import cn.mb.cloud.common.core.constant.enums.ErrorCodeConstants;
import cn.mb.cloud.common.core.exception.BusinessException;
import cn.mb.cloud.common.core.util.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
 
@Slf4j
@RestControllerAdvice
public class ControllerException {
 
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResponseData bussiness(BusinessException e) {
        log.error("业务异常 message= " + e.getMessage() + " code= " + e.getErrorCode(), e);
        return ResponseData.builder().code(ErrorCodeConstants.FAIL.getValue()).
                msg(e.getMessage()).build();
    }
 
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResponseData runtimeException(RuntimeException e) {
        log.error("运行时异常信息" + e.getMessage(), e);
        return ResponseData.builder().code(ErrorCodeConstants.FAIL.getValue()).msg(e.getMessage()).build();
    }
 
    /**
     * 全局异常.
     *
     * @param e the e
     * @return R
     */
 
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ResponseData exception(Exception e) {
        log.error("全局异常信息" + e.getMessage(), e);
        return ResponseData.fail();
    }
}