使用构建器模式创建例外?

时间:2015-04-06 08:23:58

标签: java exception-handling builder

我的java代码中有一个例外,它有几个属性可以传递给构造函数。因此我认为使用构建器模式创建异常会很好,所以我创建了这样的异常:

public class ApplicationException extends Exception {

    private static final long serialVersionUID = -8999932578270387947L;

    /** 
     * contains redundantly the HTTP status of the response sent back to the client in case of error, so that
     * the developer does not have to look into the response headers. If null a default 
     */
    Integer status;

    /** application specific error code */
    int code; 

    /** link documenting the exception */   
    String link;

    /** detailed error description for developers*/
    String developerMessage;    

    /**
     * 
     * @param status
     * @param code
     * @param message
     * @param developerMessage
     * @param link
     */
    protected ApplicationException(String message) {
        super(message);
    }

    ...

    public static Builder getBuilder(String message) {
        return new Builder(message);
    }   

    public static class Builder {
        private Integer status;
        private int code; 
        private String link;
        private String message;
        private String developerMessage;     

        public Builder(String message) {
            this.message = message;
        }

        public Builder status(int status) {
            this.status = status;
            return this;
        }


        public Builder code(int code) {
            this.code = code;
            return this;            
        }        

        public Builder developerMessage(String developerMessage) {
            this.developerMessage = developerMessage;
            return this;            
        }    

        public Builder link(String link) {
            this.link = link;
            return this;            
        }

        public ApplicationException build() {
            if (StringUtils.isBlank(message)) {
                throw new IllegalStateException("message cannot be null or empty");
            }

            if (StringUtils.isBlank(link)){
                link = AppConstants.API_SUPPORT_EMAIL;
            }

            ApplicationException created = new ApplicationException(message);
            created.status = status;
            created.code = code;
            created.developerMessage = developerMessage;
            created.link = link;

            return created;
        }        
    }
}

现在我可以创建一个这样的例外:

throw ApplicationException.getBuilder("This is the general error message")
    .status(404)
    .code(StatusCode.RESOURCE_DOES_NOT_EXIST)
    .developerMessage("The resource does not exist")
    .build();

我目前遇到的问题是我想从此基本异常中创建不同的异常。例如。 ValidationException应该从ApplicationException扩展。

但build()方法已经返回具体类型ApplicationException。目前我被困住了,因为我对使用泛型并不熟悉,甚至在异常类中也是如此。

2 个答案:

答案 0 :(得分:2)

您可以将Class<? extends Exception>传递给build方法,然后使用newInstance()创建所需类型的Exception对象:

throw ExceptionBuilder.getBuilder("This is the general error message")
    .status(404)
    .code(StatusCode.RESOURCE_DOES_NOT_EXIST)
    .developerMessage("The resource does not exist")
    .build(ValidationException.class); // <<== Choose type here
Exception调用之前,不应创建

build对象;在build内你可以这样做:

Exception build(Class<? extends Exception> eClass) {
    Exception res = null;
    try {
        res = eClass.newInstance();
    } catch (.......) {
        // Catch the appropriate exceptions here
    }
    res.setAbc(); // Set values as needed
    ...
    return res;
}

答案 1 :(得分:2)

您要处理多少种不同的异常子类型?如果它不是太多,您可以在构建器中为每种异常类型创建一个build()方法:

throw ApplicationException.getBuilder("This is the general error message")
    .status(404)
    .code(StatusCode.RESOURCE_DOES_NOT_EXIST)
    .developerMessage("The resource does not exist")
    .buildValidation();  // or buildSomeOther()

它不是通用的,但它非常简单,并且可以很好地与IDE自动完成配合使用