Skip to content

参考:错误码枚举与 i18n 资源文件定义

错误码枚举定义

java
package cn.com.digitalhainan.xxx.constants;

import cn.com.digitalhainan.common.core.exception.IBusinessExceptionCodeConfiguration;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
public enum XxxOfBusinessExceptionCodeEnum implements IBusinessExceptionCodeConfiguration {

    // 参数相关
    ERR_INVALID_PARAMS("无效请求参数"),
    ERR_REQUIRED_FIELD_MISSING("必填字段缺失: {0}"),

    // 数据相关
    ERR_DATA_NOT_FOUND("数据不存在"),
    ERR_DATA_ALREADY_EXISTS("数据已存在"),

    // 业务规则
    ERR_STATUS_NOT_ALLOWED("当前状态不允许此操作"),
    ERR_NO_PERMISSION("无权限执行此操作"),

    // 外部服务
    ERR_RPC_TIMEOUT("外部服务调用超时"),
    ERR_RPC_FAIL("外部服务调用失败: {0}");

    private String defaultMsg;

    @Override
    public String code() {
        return this.name();
    }

    @Override
    public String defaultMsg() {
        return this.defaultMsg;
    }
}

i18n 资源文件

src/main/resources/i18n/messages.properties(中文默认):

properties
ERR_INVALID_PARAMS=无效请求参数
ERR_REQUIRED_FIELD_MISSING=必填字段缺失: {0}
ERR_DATA_NOT_FOUND=数据不存在
ERR_DATA_ALREADY_EXISTS=数据已存在
ERR_STATUS_NOT_ALLOWED=当前状态不允许此操作
ERR_NO_PERMISSION=无权限执行此操作
ERR_RPC_TIMEOUT=外部服务调用超时
ERR_RPC_FAIL=外部服务调用失败: {0}

src/main/resources/i18n/messages_en.properties(英文):

properties
ERR_INVALID_PARAMS=Invalid request parameters
ERR_REQUIRED_FIELD_MISSING=Required field missing: {0}
ERR_DATA_NOT_FOUND=Data not found
ERR_DATA_ALREADY_EXISTS=Data already exists
ERR_STATUS_NOT_ALLOWED=Operation not allowed in current status
ERR_NO_PERMISSION=No permission to perform this operation
ERR_RPC_TIMEOUT=External service call timed out
ERR_RPC_FAIL=External service call failed: {0}

Power By 数字海南