success 已正常处理 hide_PHP短视频源码,全局异常处理

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

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值