Air
2024-11-04 2016903d06e308f44b9463fcd4029850ababf152
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
package com.nova.sankuai.infra.config;
 
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
 
/**
 * @author weikangdi
 * 业务异常处理
 */
@Getter
public class CommonException extends RuntimeException {
    private static final Logger logger = LoggerFactory.getLogger(CommonException.class);
    private static final long serialVersionUID = 5044938065901970022L;
    private final transient Object[] parameters;
    private String code;
 
    public CommonException(String code, Object... parameters) {
        super(code);
        this.parameters = parameters;
        this.code = code;
    }
 
    public CommonException(String code, Throwable cause, Object... parameters) {
        super(code, cause);
        this.parameters = parameters;
        this.code = code;
    }
 
    public CommonException(String code, Throwable cause) {
        super(code, cause);
        this.code = code;
        this.parameters = new Object[0];
    }
 
    public CommonException(Throwable cause, Object... parameters) {
        super(cause);
        this.parameters = parameters;
    }
 
    public String getTrace() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = null;
 
        label57:
        {
            Object var4;
            try {
                ps = new PrintStream(baos, false, StandardCharsets.UTF_8.name());
                break label57;
            } catch (UnsupportedEncodingException var8) {
                logger.error("Error get trace, unsupported encoding.", var8);
                var4 = null;
            } finally {
                if (ps != null) {
                    ps.close();
                }
 
            }
 
            return (String) var4;
        }
 
        this.printStackTrace(ps);
        ps.flush();
        return new String(baos.toByteArray(), StandardCharsets.UTF_8);
    }
 
    public Map<String, Object> toMap() {
        HashMap<String, Object> map = new LinkedHashMap();
        map.put("code", this.code);
        map.put("message", super.getMessage());
        return map;
    }
}