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

org.cattleframework.webmvc.exception.ExceptionAdvice Maven / Gradle / Ivy

There is a newer version: 1.0.1-M3
Show newest version
/*
 * Copyright (C) 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cattleframework.webmvc.exception;

import org.apache.commons.lang3.StringUtils;
import org.cattleframework.exception.web.ExceptionProcessCustomizer;
import org.cattleframework.web.WebConstants;
import org.cattleframework.web.WebProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.net.HttpHeaders;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * 异常Advice
 * 
 * @author orange
 *
 */
@ControllerAdvice
public class ExceptionAdvice {

    private final ErrorAttributes errorAttributes;

    private final WebProperties webProperties;

    private final ExceptionProcessCustomizer[] exceptionProcessCustomizers;

    @Value("${" + WebConstants.PAGE_USE_TEMPLATE + ":false}")
    private boolean pageUseTemplate;

    public ExceptionAdvice(ErrorAttributes errorAttributes, WebProperties webProperties,
	    ExceptionProcessCustomizer[] exceptionProcessCustomizers) {
	this.errorAttributes = errorAttributes;
	this.webProperties = webProperties;
	this.exceptionProcessCustomizers = exceptionProcessCustomizers;
    }

    @ExceptionHandler(Throwable.class)
    public ModelAndView processThrowable(HttpServletRequest request, HttpServletResponse response,
	    Throwable exception) {
	String acceptHeader = request.getHeader(HttpHeaders.ACCEPT);
	boolean isHtml = StringUtils.contains(acceptHeader, MediaType.TEXT_HTML_VALUE);
	return ExceptionHandlerUtils.getExceptionModelAndView(request, response, errorAttributes, webProperties, isHtml,
		pageUseTemplate, exceptionProcessCustomizers);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy