AbstractException.java
1.94 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
74
75
76
77
78
79
80
/*
* Copyright (c) 2018.
* 项目名称:auth-gateway-backend
* 文件名称:AbstractException.java
* Date:18-3-21 上午10:15
* Author:boni
*/
package com.irrigation.icl.exception;
import com.irrigation.icl.exception.code.ErrorCode;
import org.springframework.http.HttpStatus;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* @author zhangchuanxi
* @Description 自定义抽象异常类
*/
public abstract class AbstractException extends RuntimeException {
private static final long serialVersionUID = 1L;
private final HttpStatus status;
private final int code;
private final String message;
private final String detailMessage;
public AbstractException(HttpStatus status, ErrorCode errorCode) {
this.status = status;
this.code = errorCode.getCode();
this.message = errorCode.getMessage();
this.detailMessage = errorCode.getDetailMessage();
}
public AbstractException(HttpStatus status) {
this.status = status;
code = 0;
message = null;
detailMessage = null;
}
public AbstractException(HttpStatus status, ErrorCode errorCode, Throwable cause) {
super(cause);
this.status = status;
this.code = errorCode.getCode();
this.message = errorCode.getMessage();
detailMessage = (cause.getMessage() != null ? cause.getMessage() : "") + toStackTrace(cause);
}
private String toStackTrace(Throwable e) {
StringWriter errorStackTrace = new StringWriter();
e.printStackTrace(new PrintWriter(errorStackTrace));
return errorStackTrace.toString();
}
public HttpStatus getStatus() {
return status;
}
public ErrorCode getCode() {
return new ErrorCode(code, message, detailMessage);
}
@Override
public String getMessage() {
return message;
}
public String getDetailMessage() {
return detailMessage;
}
}