Blame view

src/main/java/com/canrd/webmagic/common/exception/BusinessException.java 2.39 KB
谢茂盛 authored
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
package com.canrd.webmagic.common.exception;

import com.canrd.webmagic.common.constant.ServerResult;
import com.canrd.webmagic.common.constant.ServerResultCode;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>基础异常类,所有自定义异常类都需要继承本类</p>
 */
@Slf4j
public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = 3152549963899218489L;
    @Setter
    @Getter
    private String errorDesc;
    @Setter
    @Getter
    private Integer errorCode;
    @Setter
    @Getter
    private Object data;

    public BusinessException() {
        super(ServerResultCode.FAIL.getErrorDesc());
        this.errorCode = ServerResultCode.FAIL.getErrorCode();
        this.errorDesc = ServerResultCode.FAIL.getErrorDesc();
    }

    public BusinessException(int errorCode, String errorDesc) {
        super(errorDesc);
        this.errorCode = errorCode;
        this.errorDesc = errorDesc;
    }

    public BusinessException(ServerResultCode serverResultCode) {
        super(serverResultCode.getErrorDesc());
        this.errorCode = serverResultCode.getErrorCode();
        this.errorDesc = serverResultCode.getErrorDesc();
        log.error("业务异常: ", this);
    }

    public BusinessException(ErrorInfo errorInfo) {
        super(errorInfo.getErrorDesc());
        this.errorCode = errorInfo.getErrorCode();
        this.errorDesc = errorInfo.getErrorDesc();
        log.error("业务异常: ", this);
    }

    public BusinessException(ServerResult<?> serverResult) {
        super(serverResult == null ? ServerResultCode.FAIL.getErrorDesc() : serverResult.getMessage());
        if (serverResult == null) {
            this.errorCode = ServerResultCode.FAIL.getErrorCode();
            this.errorDesc = ServerResultCode.FAIL.getErrorDesc();
        } else {
            this.errorCode = serverResult.getResult();
            this.errorDesc = serverResult.getMessage();
            this.data = serverResult.getData();
        }
        log.error("业务异常: ", this);
    }

    public BusinessException(String message) {
        super(message);
    }

    public BusinessException(Throwable cause) {
        super(cause);
        log.error("业务异常: ", this);
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
        log.error("业务异常: ", this);
    }
}