前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP短视频源码,全局异常处理

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

原创
作者头像
yunbaokeji柯基
修改2020-11-10 17:47:39
6880
修改2020-11-10 17:47:39
举报
文章被收录于专栏:直播知识直播知识

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短视频源码,全局异常处理相关的代码, 更多内容欢迎关注之后的文章

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档