package com.dy.common.webUtil; 
 | 
  
 | 
import io.swagger.v3.oas.annotations.Hidden; 
 | 
import io.swagger.v3.oas.annotations.responses.ApiResponse; 
 | 
import io.swagger.v3.oas.annotations.responses.ApiResponses; 
 | 
import io.swagger.v3.oas.annotations.tags.Tag; 
 | 
import jakarta.servlet.RequestDispatcher; 
 | 
import jakarta.servlet.http.HttpServletRequest; 
 | 
import org.springframework.boot.web.servlet.error.ErrorController; 
 | 
import org.springframework.web.bind.annotation.RequestMapping; 
 | 
import org.springframework.web.bind.annotation.RequestMethod; 
 | 
import org.springframework.web.bind.annotation.RestController; 
 | 
  
 | 
/** 
 | 
 * 全局出错处理,web请求进入Controller之前发生的错误及异常都由此类处理, 
 | 
 * 例如请求路径不正确,以及在Filter中抛出的异常等 
 | 
 * 而在Controller中抛出的异常由GlExceptionHandler或GlExceptionAspect处理 
 | 
 */ 
 | 
//此错误处理类并不对外提供服务,所以其不用暴露接口 
 | 
@Hidden //在SpringDoc中隐藏本接口API 
 | 
@Tag(name = "全局出错处理", description = "web请求进入Controller之前发生的错误及异常都由此类处理") 
 | 
@ApiResponses(@ApiResponse(responseCode = ResultCodeMsg.RsCode.ERROR_CODE, description = "返回出错信息")) 
 | 
@RequestMapping(produces = "application/json") 
 | 
@RestController 
 | 
public class GlErrorCtrl implements ErrorController { 
 | 
    //此错误处理类并不对外提供服务,所以其不用暴露接口 
 | 
    //@Operation(summary = "错误处理方式", description = "返回BaseResponse类型的数据") 
 | 
    @RequestMapping(value = "/error", method = RequestMethod.GET) 
 | 
    public BaseResponse error(HttpServletRequest request) { 
 | 
        // get error status code 
 | 
        Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); 
 | 
        /* 
 | 
        if (status != null) { 
 | 
            // handle 404 error 
 | 
            if (status.toString().equals("404")) { 
 | 
                return BaseResponseUtils.buildFail("error-404"); 
 | 
            } 
 | 
            // handle 500 error 
 | 
            else if (status.toString().equals("500")) { 
 | 
                return BaseResponseUtils.buildFail("error-500"); 
 | 
            } 
 | 
        } 
 | 
        */ 
 | 
        return BaseResponseUtils.buildFail("进入Controller之前发生错误" + (status==null?"":(",错误码:" + status.toString()))) ; 
 | 
    } 
 | 
  
 | 
} 
 |