美文网首页架构
spring项目结构设计-错误和异常

spring项目结构设计-错误和异常

作者: jinelei | 来源:发表于2020-05-13 16:14 被阅读0次
    error and exception

    结构说明:提供错误信息接口ErrorIntf,具体的实现类为BasicError,异常类BasicException被设计为继承自RuntimeException并拥有自己的属性error,实例化时将ErrorIntf当作参数传入。

    • 优点:
    1. 规范错误码(限制再枚举类中取值)
    2. 可以附带错误信息(通过BasicExceptiondesc属性)
    3. spring全局统一异常处理(可以包装其他异常,通过BasicExceptionthrowable属性
    • 缺点:
    1. spring全局异常处理不能很方便的根据异常类型来处理返回值,需要将错误实例中的error取出来根据errCode来处理
    • 示例:

    new BasicException(BasicError.METHOD_NOT_IMPLEMENTATION, "method name: mergeEntity")

    • 错误类标准接口ErrorIntf
    /**
    * 标准错误接口
    */
    public interface ErrorIntf {
    
       /**
        * 获取错误代码
        */
       Integer getErrorCode();
    
       /**
        * 获取错误信息
        */
       String getErrorMessage();
       
    }
    
    • 标准错误类BasicError
    public enum BasicError implements ErrorIntf {
        
        /** 一般错误 **/
        BAD_PARAM(100000, "BAD_PARAM"),
        CONVERT_FAILED(100001, "CONVERT_FAILED"),
        NUMBER_FORMAT_ERROR(100002, "NUMBER_FORMAT_ERROR"),
        LOST_PRECISION_FAILED(100003, "LOST_PRECISION_FAILED"),
        BEAN_BIND_FAILED(100004, "BEAN_BIND_FAILED"),
        PERMISSION_DENIED(100005, "PERMISSION_DENIED"),
        INVALID_ENUM_TYPE(100006, "INVALID_ENUM_TYPE"),
        METHOD_NOT_IMPLEMENTATION(100007, "METHOD_NOT_IMPLEMENTATION"),
        FIELD_NOT_FOUND(100008, "FIELD_NOT_FOUND"),
        METHOD_NOT_FOUND(100009, "METHOD_NOT_FOUND"),
        PARAM_NOT_FOUND(100010, "PARAM_NOT_FOUND"),
        CLASS_NOT_FOUND(100011, "CLASS_NOT_FOUND"),
        INTF_NOT_FOUND(100012, "INTF_NOT_FOUND"),
        ENUM_NOT_FOUND(100013, "ENUM_NOT_FOUND"),
        CLASS_TYPE_NOT_SUPPORT(100014, "CLASS_TYPE_NOT_SUPPORT"),
        METHOD_TYPE_NOT_SUPPORT(100015, "METHOD_TYPE_NOT_SUPPORT"),
        OBJECT_TYPE_NOT_SUPPORT(100016, "OBJECT_TYPE_NOT_SUPPORT"),
        BAD_REQUEST(100017, "BAD_REQUEST"),
        UNSUPPORTED_MEDIA_TYPE(100018, "UNSUPPORTED_MEDIA_TYPE"),
        INTERNAL_SERVER_ERROR(100019, "INTERNAL_SERVER_ERROR"),
    
        /** 数据错误 **/
        DATA_INSERT_FAILED(200000, "DATA_INSERT_FAILED"),
        DATA_DELETE_FAILED(200001, "DATA_DELETE_FAILED"),
        DATA_UPDATE_FAILED(200002, "DATA_UPDATE_FAILED"),
        DATA_QUERY_FAILED(200003, "DATA_QUERY_FAILED"),
        DATA_NOT_EXIST(200004, "DATA_NOT_EXIST"),
        DATA_ALREADY_EXIST(200005, "DATA_ALREADY_EXIST"),
        DATA_NOT_UNIQUE_EXIST(200006, "DATA_NOT_UNIQUE_EXIST"),
    
    
        /** 文件错误 **/
        FILE_NOT_EXIST(300000, "FILE_NOT_EXIST"),
        FILE_ALREADY_EXIST(300001, "FILE_ALREADY_EXIST"),
        FILE_NOT_READABLE(300002, "FILE_NOT_READABLE"),
        FILE_NOT_WRITEABLE(300003, "FILE_NOT_WRITEABLE"),
        FILE_PERMISSION_DENIED(300004, "FILE_PERMISSION_DENIED"),
        DIRECTORY_NOT_EXIST(300005, "DIRECTORY_NOT_EXIST"),
        DIRECTORY_ALREADY_EXIST(300006, "DIRECTORY_ALREADY_EXIST"),
        DIRECTORY_NOT_READABLE(300007, "DIRECTORY_NOT_READABLE"),
        DIRECTORY_NOT_WRITEABLE(300008, "DIRECTORY_NOT_WRITEABLE"),
        DIRECTORY_PERMISSION_DENIED(300009, "DIRECTORY_PERMISSION_DENIED"),
        DIRECTORY_OPERATION_FAILED(300010, "DIRECTORY_OPERATION_FAILED"),
        DIRECTORY_CREATE_FAILED(300011, "DIRECTORY_CREATE_FAILED"),
        DIRECTORY_DELETE_FAILED(300012, "DIRECTORY_DELETE_FAILED"),
        DIRECTORY_UPDATE_FAILED(300013, "DIRECTORY_UPDATE_FAILED"),
        FILE_CREATE_FAILED(300014, "FILE_CREATE_FAILED"),
        FILE_DELETE_FAILED(300015, "FILE_DELETE_FAILED"),
        FILE_UPDATE_FAILED(300016, "FILE_UPDATE_FAILED"),
        FILE_READ_FAILED(300017, "FILE_READ_FAILED"),
        FILE_WRITE_FAILED(300018, "FILE_WRITE_FAILED"),
        FILE_OPERATION_FAILED(300019, "FILE_OPERATION_FAILED"),
    
        /** 网络错误 **/
        HTTP_OPERATION_FAILED(900050, "HTTP_OPERATION_FAILED"),
    
        /** 默认错误 **/
        UNKNOWN_ERROR(999999, "UNKNOWN_ERROR");
    
        private Integer errorCode;
        private String errorMessage;
    
        private BassError(Integer errorCode, String errorMessage){
            this.errorCode = errorCode;
            this.errorMessage = errorMessage;
        }
    
        @Override
        public Integer getErrorCode() {
            return this.errorCode;
        }
    
        @Override
        public String getErrorMessage() {
            return this.errorMessage;
        }
        
    }
    
    • 异常类的设计
    public class BasicException extends RuntimeException {
        private static final long serialVersionUID = 1046400807995227718L;
        private ErrorIntf error;
    
        public ErrorIntf getError() {
            return error;
        }
    
        public BasicException(ErrorIntf error) {
            super(error.getErrCode() + " " + error.getErrMsg());
            this.error = error;
        }
    
        public BasicException(ErrorIntf error, String message) {
            super(message);
            this.error = error;
        }
    
        public BasicException(ErrorIntf error, Throwable throwable) {
            super(throwable);
            this.error = error;
        }
    
        public BasicException(ErrorIntf error, Throwable throwable, String message) {
            super(message, throwable);
            this.error = error;
        }
    
        public BasicException(ErrorIntf error, Throwable throwable, String message, boolean enableSuppression, boolean writableStackTrace) {
            super(message, throwable, enableSuppression, writableStackTrace);
            this.error = error;
        }
    
    }
    

    相关文章

      网友评论

        本文标题:spring项目结构设计-错误和异常

        本文链接:https://www.haomeiwen.com/subject/hbnnnhtx.html