PHP短视频源码,全局异常处理

2020-11-10 17:47:39 浏览数 (1)

PHP短视频源码,全局异常处理相关的代码

代码语言:javascript复制
package com.chashiyu.configuration;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
 * @author shiyu.zha on 2019-09-29.
 * @see ModelAttribute 把值绑定到Model中,使全局@RequestMapping可以获取到该值
 * @see ExceptionHandler 指定拦截异常的类型
 * @see ResponseStatus 自定义浏览器返回状态码
 */
@Slf4j
@RestControllerAdvice
public class MyExceptionHandler {
    @Data
    public class ResultData {
        private int code;
        private String message;
        private Object data;
        public ResultData(Object data) {
            this.code = RespCode.SUCCESS.getCode();
            this.message = RespCode.SUCCESS.getMessage();
            this.data = data;
        }
    }
     //    @ModelAttribute
//    public void addAttribute(Model model) {//        model.addAttribute("attribute", "The Attribute");//    }    
@ExceptionHandler({CustomException.class})    public ResultData custom(CustomException e) {        log.error("自定义异常-->{}: {}", e.getRespCode().getCode(), e.getRespCode().getMessage());        return new ResultData(e.getRespCode());    }    @ExceptionHandler({Exception.class})    public ResultData global(Exception e) {        log.error("全局异常-->{}: {}", RespCode.SERVER_ERROR.getCode(), RespCode.SERVER_ERROR.getMessage(), e);        return new ResultData(RespCode.SERVER_ERROR);    }    public class CustomException extends RuntimeException {        private static final long serialVersionUID = 1L;        private RespCode respCode;        public CustomException(RespCode respCode) {            super(respCode.getMessage());            this.respCode = respCode;        }        public CustomException(RespCode respCode, String message) {            super(message);            this.respCode = respCode;        }        public CustomException(RespCode respCode, Throwable t) {            super(respCode.getMessage(), t);            this.respCode = respCode;        }        public CustomException(RespCode respCode, String message, Throwable t) {            super(message, t);            this.respCode = respCode;        }        public RespCode getRespCode() {            return respCode;        }    }
public enum RespCode {
        SUCCESS(200, "success"),
        SERVER_ERROR(1000, "服务异常"),         //代码未捕获、或不能捕获
        PARAM_ERROR(1001, "参数错误"),          //代码中已捕获
        SYSTEM_ERROR(1002, "系统错误"),         //代码中已捕获
        AUTH_FAILED(1003, "验权失败");          //代码中已捕获
        private int code;
        private String message;
        RespCode(int code, String message) {
            this.code = code;
            this.message = message;
        }
        public static String getByCode(Integer code) {
            RespCode[] values = RespCode.values();
            for (RespCode value : values) {
                if (code.equals(value.code))
                    return value.message;
            }
            return null;
        }
        public static String getMessage(RespCode resultCode) {
            RespCode[] values = RespCode.values();
            for (RespCode value : values) {
                if (resultCode.equals(value))
                    return resultCode.message;
            }
            return null;
        }
        public int getCode() {
            return code;
        }
        public void setCode(int code) {
            this.code = code;
        }
        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
    }
}

以上就是PHP短视频源码,全局异常处理相关的代码, 更多内容欢迎关注之后的文章

0 人点赞