หน้าเว็บ

วันพุธที่ 25 ธันวาคม พ.ศ. 2556

ExceptionBuilder java


เป็น code ที่ผมเขียนเพื่อเอาไว้สร้าง exception ให้ user ได้เห็นครับ  ว่าตอนนั้นมีข้อผิดพลาดอะไรเกิดขึ้นกับระบบ 


ExceptionBuilder.java
package com.blogspot.na5cent.exceptions;

/**
 *
 * @author recrow
 * create 25/12/2013
 */
public class ExceptionBuilder {

    private final Exception ex;
    private int code = 500;
    private String name = null;
    private String locationOccur = null;
    private StringBuilder messageBuilder = null;

    private ExceptionBuilder(Exception ex) {
        this.ex = ex;
        this.name = ex.getClass().getName();
    }

    public static ExceptionBuilder buildFormException(Exception ex) {
        return new ExceptionBuilder(ex);
    }

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

    public ExceptionBuilder setLocationOccur(String location) {
        this.locationOccur = location;
        return this;
    }

    public ExceptionBuilder addMessage(String message) {
        if(messageBuilder == null){
            messageBuilder = new StringBuilder();
        }
        
        messageBuilder.append(message);
        return this;
    }
    
    
    private boolean isEmpty(String text){
        return text == null || text.isEmpty();
    }

    public void build() {
        StringBuilder builder = new StringBuilder();
        builder.append("<span class=\"ns-error-message-label\">ประเภทข้อผิดพลาด</span> : ")
                  .append(name)
                  .append("<br/>")
                  .append("<span class=\"ns-error-message-label\">รหัสข้อผิดพลาด</span> : ")
                  .append(code)
                  .append("<br/>");

        if (!isEmpty(locationOccur)) {
            builder.append("<span class=\"ns-error-message-label\">เกิดขึ้นที่</span> : ")
                      .append(locationOccur)
                      .append("<br/>");
        }

        String message = messageBuilder.toString();
        if (!isEmpty(message)) {
            builder.append("<span class=\"ns-error-message-label\">ข้อความ</span> : ")
                      .append(message)
                      .append("<br/>");
        }
        
        messageBuilder = null;
        throw new RuntimeException(builder.toString(), ex);
    }
}
สร้าง ExceptionBuilderFactory สำหรับผลิต ExceptionBuilder สำเร็จรูปแต่ละประเภท
ExceptionBuilderFactory.java
package com.blogspot.na5cent.exceptions;

import com.blogspot.na5cent.exceptions.UserException;

/**
 *
 * @author recrow
 * create 25/12/2013
 */
public class ExceptionBuilderFactory {

    private String locationOccur = null;

    public ExceptionBuilderFactory(Class<?> clazz) {
        locationOccur = clazz.getName();
    }

    public ExceptionBuilder createExceptionBuilder(Exception ex) {
        ExceptionBuilder builder;
        if (ex instanceof UserException) {
            builder = ExceptionBuilder.buildFormException(ex)
                    .setExceptionCode(54100)
                    .setLocationOccur(locationOccur);
        } else if(.....){
            //....
        } else
            builder = ExceptionBuilder.buildFormException(ex)
                    .setLocationOccur(locationOccur);
        }

        return builder;
    }
}
Excample
OpdContentAuditForm.java
...
...
    private final ExceptionBuilderFactory exceptionBuilderFactory = new ExceptionBuilderFactory(OpdContentAuditForm.class);
    private final ExceptionBuilder userExceptionBuilder = exceptionBuilderFactory.createExceptionBuilder(new UserException());



    userExceptionBuilder.addMessage("ไม่พบข้อมูลเวชระเบียน hcode=" + hcode)
                    .addMessage(", pid=" + pid)
                    .addMessage(", budgetYear=" + budgetYear)
                    .addMessage(", diseaseId=" + diseaseId)
                    .build(); //throw an exception
...
...

ไม่มีความคิดเห็น:

แสดงความคิดเห็น