[unified global exception handling] 2. User defined exception and related data structure

Keywords: Programming

1, How to design data structure

  1. CustomException custom exception. Core elements: abnormal error code (200 normal, 400500), abnormal error message.
  2. ExceptionTypeEnum enumerates exception classifications, solidifies exception classifications, and prevents developers from thinking divergently. Core element abnormal classification code (200 normal, 400500), abnormal classification description.
  3. Ajax response is used to respond to Ajax requests. Core elements: whether the request is successful isok; response code zero and non-zero, zero means success (200400500); response success or not information description message; response success data.
  4. error.html
    In addition, there needs to be a unified place to handle CustomException. Namely @ ControllerAdvice and @ ExceptionHandler

2, Custom exception and custom response data structure

Enumeration of exception classification, solidifying the exception classification

public enum CustomExceptionType {
    /**
     * Defining exceptions
     */
    USER_INPUT_ERROR(400, "User input exception"),
    SYSTEM_ERROR(500, "System service exception"),
    OTHER_ERROR(999, "Other unknown exception");

    CustomExceptionType(int code, String typeDesc) {
        this.code = code;
        this.typeDesc = typeDesc;
    }

    /**
     * Exception type Chinese description
     */
    private String typeDesc;

    /**
     * code  Status code
     */
    private int code;

    public String getTypeDesc() {
        return typeDesc;
    }

    public int getCode() {
        return code;
    }
}

 

Custom exception

public class CustomException extends RuntimeException {
    /**
     * Abnormal error code
     */
    private int code;

    /**
     * Abnormal information
     */
    private String message;

    /**
     * User exception
     *
     * @param exceptionTypeEnum
     * @param message
     */
    public CustomException(CustomExceptionType exceptionTypeEnum, String message) {
        this.code = exceptionTypeEnum.getCode();
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

}

 

Unified response front end data structure

@Data
public class AjaxResponse {
    /**
     * ajax Is the request successful
     */
    private boolean isok;
    /**
     * http status code
     */
    private int code;
    /**
     * Prompt for request failure.
     */
    private String message;
    /**
     * When the request is successful, the data to the front end needs to be responded to
     */
    private Object data;

    private AjaxResponse() {

    }

    /**
     * Response data encapsulation in case of request exception
     * @param e
     * @return
     */
    public static AjaxResponse error(CustomException e) {

        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setIsok(false);
        resultBean.setCode(e.getCode());
        if(e.getCode() == CustomExceptionType.USER_INPUT_ERROR.getCode()){
            resultBean.setMessage(e.getMessage());
        }else if(e.getCode() == CustomExceptionType.SYSTEM_ERROR.getCode()){
            resultBean.setMessage(e.getMessage() + ",There is an exception in the system. Please contact the administrator for handling!");
        }else{
            resultBean.setMessage("There is an unknown exception in the system. Please contact the administrator for handling!");
        }
        return resultBean;
    }

    /**
     * Response data encapsulation when the request is successful, no response data (for example, deletion and modification are successful)
     * @return
     */
    public static AjaxResponse success() {
        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setIsok(true);
        resultBean.setCode(200);
        resultBean.setMessage("success");
        return resultBean;
    }

    /**
     * Response data encapsulation when the request is successful, with response data (such as query success)
     * @param data
     * @return
     */
    public static AjaxResponse success(Object data) {
        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setIsok(true);
        resultBean.setCode(200);
        resultBean.setMessage("success");
        resultBean.setData(data);
        return resultBean;
    }
}

 

3, How to use ajax response without exception

For example: update operation, Controller does not need to return additional data

    @PutMapping("/api/custom/{id}")
    public AjaxResponse putCustom(@PathVariable("id") Long id) {
        return AjaxResponse.success();
    }

For example: query interface, Controller needs to return result data (data can be any type of data)

    @GetMapping("/api/custom/{id}")
    public AjaxResponse getCustom(@PathVariable("id") Long id) {
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("customId", id);
        m.put("customName", "Li Si");
        m.put("customAge", "female");

        return AjaxResponse.success(m);
    }

Posted by phychion on Thu, 26 Dec 2019 06:41:36 -0800