WangHan
2024-09-12 d5855a4926926698b740bc6c7ba489de47adb68b
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
package tech.powerjob.common.response;
 
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.exception.ExceptionUtils;
import tech.powerjob.common.enums.ErrorCodes;
import tech.powerjob.common.exception.PowerJobException;
 
/**
 * 新的 Result,带状态码
 *
 * @author 程序帕鲁
 * @since 2024/2/19
 */
@Getter
@Setter
public class PowerResultDTO<T> extends ResultDTO<T> {
 
    private String code;
 
    public static <T> PowerResultDTO<T> s(T data) {
        PowerResultDTO<T> r = new PowerResultDTO<>();
        r.success = true;
        r.data = data;
        return r;
    }
 
    public static <T> PowerResultDTO<T> f(String message) {
        PowerResultDTO<T> r = new PowerResultDTO<>();
        r.success = false;
        r.message = message;
        return r;
    }
 
    public static <T> PowerResultDTO<T> f(Throwable t) {
        PowerResultDTO<T> f = f(ExceptionUtils.getStackTrace(t));
        f.setCode(ErrorCodes.SYSTEM_UNKNOWN_ERROR.getCode());
        return f;
    }
 
    public static <T> PowerResultDTO<T> f(PowerJobException pje) {
        PowerResultDTO<T> f = f(pje.getMessage());
        f.setCode(pje.getCode());
        return f;
    }
 
}