All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nxyfan.framework.common.exception.CommonErrorViewController Maven / Gradle / Ivy

package com.nxyfan.framework.common.exception;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nxyfan.framework.common.pojo.CommonResult;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常页面处理器,覆盖默认的Whitelabel Error Page
 *
 * @author amour
 * @date 2022/2/11 15:41
 **/
@Slf4j
@RestController
public class CommonErrorViewController {

    /**
     * Error页面视图,直接响应JSON
     *
     * @author amour
     * @date 2022/2/11 16:11
     **/
    @RequestMapping("/errorView")
    public CommonResult globalError(HttpServletRequest request) {
        CommonResult commonResult = new CommonResult<>(404, "路径不存在", null);
        Object model = request.getAttribute("model");
        if(ObjectUtil.isNotEmpty(model)) {
            JSONObject errorObj = JSONUtil.parseObj(model);
            Integer code = errorObj.getInt("rspCode");
            String msg = errorObj.getStr("rspMsg");
            if(ObjectUtil.isAllNotEmpty(code, msg)) {
                commonResult.setRspCode(code).setRspMsg(msg);
            } else if(ObjectUtil.isNotEmpty(msg)) {
                commonResult = CommonResult.error(msg);
            } else {
                commonResult = CommonResult.error();
            }
            //Exception exception = (Exception) model;
            //exception.printStackTrace();
        }
        log.error(">>> {}", commonResult.getRspMsg());
        return commonResult;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy