kongdeqiang
2026-10-12 ccf1a44c001e233c158bd168dc2d0d96721af929
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.custom.webui.exception;
 
import lombok.Getter;
 
/**
 * 业务异常。由 {@link GlobalExceptionHandler} 统一转换成 {@code Result} 结构。
 *
 * <p>用 HTTP 状态码表达语义:401 未认证、403 无权限、404 不存在、400 参数错误、500 服务端错误。
 */
@Getter
public class BusinessException extends RuntimeException {
 
    private final int code;
 
    public BusinessException(int code, String message) {
        super(message);
        this.code = code;
    }
 
    public static BusinessException badRequest(String message) {
        return new BusinessException(400, message);
    }
 
    public static BusinessException unauthorized(String message) {
        return new BusinessException(401, message);
    }
 
    public static BusinessException forbidden(String message) {
        return new BusinessException(403, message);
    }
 
    public static BusinessException notFound(String message) {
        return new BusinessException(404, message);
    }
 
    public static BusinessException serverError(String message) {
        return new BusinessException(500, message);
    }
}