package com.hzya.frame.webapp.web.exception;

/**
 * @Content
 * @Author 👻👻👻👻👻👻👻👻👻👻 gjh
 * @Date 2022-12-06 9:40
 * @Version 1.0
 */

import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import com.hzya.frame.web.exception.BaseSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Description:
 * @ClassName:spring_boot_j220601
 * @Author:。。。。
 * @CreateDate:2022-09-20 16:12:03
 **/
@SuppressWarnings("ALL")
@Controller
@RequestMapping({"${server.error.path:${error.path:/error}}"})
public class MyBasicErrorController implements ErrorController {
    @Autowired
    private ErrorAttributes errorAttributes;


    @RequestMapping(CommonConstant.ERROR_CONTROLLER_PATH)
    public void handleException(HttpServletRequest request) {
        throw (BaseSystemException) request.getAttribute("filterError");
    }

    // 页面请求异常
    @RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
        throw new BaseSystemException("系统异常");
    }

    // 接口请求异常
    @RequestMapping
    @ResponseBody
    public JsonResultEntity error(HttpServletRequest request) {
        Map<String, Object> body = getErrorAttributes(request, this.getErrorAttributeOptions(request, MediaType.ALL));
        body.get("status");
        body.get("error");
        body.get("path");
        body.get("message");
        StringBuffer messageBuffer = new StringBuffer();
        messageBuffer.append("系统异常！请联系系统管理员。错误信息：");
        messageBuffer.append(body.get("error"));
        messageBuffer.append(body.get("path"));
        return BaseResult.getFailureMessageEntity(messageBuffer.toString(), String.valueOf(body.get("status")));
    }

    protected Map<String, Object> getErrorAttributes(HttpServletRequest request, ErrorAttributeOptions options) {
        WebRequest webRequest = new ServletWebRequest(request);
        return this.errorAttributes.getErrorAttributes(webRequest, options);
    }

    protected HttpStatus getStatus(HttpServletRequest request) {
        // 获取状态值
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        } else {
            try {
                return HttpStatus.valueOf(statusCode);
            } catch (Exception var4) {
                return HttpStatus.INTERNAL_SERVER_ERROR;
            }
        }
    }

    protected ErrorAttributeOptions getErrorAttributeOptions(HttpServletRequest request, MediaType mediaType) {
        ErrorAttributeOptions options = ErrorAttributeOptions.defaults();
//        if (this.errorProperties.isIncludeException()) {
//            options = options.including(new ErrorAttributeOptions.Include[]{ErrorAttributeOptions.Include.EXCEPTION.EXCEPTION});
//        }
//
//        if (this.isIncludeStackTrace(request, mediaType)) {
//            options = options.including(new ErrorAttributeOptions.Include[]{Include.STACK_TRACE});
//        }
//
//        if (this.isIncludeMessage(request, mediaType)) {
//            options = options.including(new ErrorAttributeOptions.Include[]{Include.MESSAGE});
//        }
//
//        if (this.isIncludeBindingErrors(request, mediaType)) {
//            options = options.including(new ErrorAttributeOptions.Include[]{Include.BINDING_ERRORS});
//        }

        return options;
    }

}



