kaiyu
2021-03-10 5097d13418e9a0bf605f5272f1b9e60fc62c80cb
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
package com.moral.api.exception;
 
import com.moral.constant.Constants;
import com.moral.constant.ResultMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
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
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 处理全部异常
     */
    @ExceptionHandler
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage handleException(Exception ex) {
        log.error(ex.getMessage());
        log.error(ex.getStackTrace().toString());
        return  ResultMessage.fail(Constants.CODE_OPERATION_FAILED, "请求失败");
    }
 
    /**
     * 处理BusinessException异常
     */
    @ExceptionHandler({BusinessException.class})
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage handleUserNotExistException(BusinessException ex) {
        return ResultMessage.fail(Constants.CODE_OPERATION_FAILED, "请求用户数据失败");
    }
 
}