Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <error-page>
- <location>/500</location>
- </error-page>
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.autoconfigure.web.AbstractErrorController;
- import org.springframework.boot.autoconfigure.web.ErrorAttributes;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import javax.servlet.http.HttpServletRequest;
- @Controller
- public class CustomErrorController extends AbstractErrorController {
- public static final String ERROR_500 = "/500";
- private static final String ERROR_PATH= "/error";
- @Autowired
- public CustomErrorController(ErrorAttributes errorAttributes) {
- super(errorAttributes);
- }
- /**
- * Responsible for handling all errors and throw especial exceptions
- * for some HTTP status codes. Otherwise, it will return a map that
- * ultimately will be converted to a json error.
- */
- @RequestMapping({ERROR_PATH,ERROR_500})
- public ResponseEntity<?> handleErrors(HttpServletRequest request) {
- return ResponseEntity.status(getStatus(request)).body(getErrorAttributes(request, false));
- }
- @Override
- public String getErrorPath() {
- return ERROR_PATH;
- }
- }
- /**
- * Process this request, publishing an event regardless of the outcome.
- * <p>The actual event handling is performed by the abstract
- * {@link #doService} template method.
- */
- protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- long startTime = System.currentTimeMillis();
- Throwable failureCause = null;
- // Here the locale is determined
- LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
- LocaleContext localeContext = buildLocaleContext(request);
- RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
- ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);
- WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
- asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());
- initContextHolders(request, localeContext, requestAttributes);
- try {
- // here is where the WebMvc processing happens
- doService(request, response);
- }
- catch (ServletException ex) {
- failureCause = ex;
- throw ex;
- }
- catch (IOException ex) {
- failureCause = ex;
- throw ex;
- }
- catch (Throwable ex) {
- failureCause = ex;
- throw new NestedServletException("Request processing failed", ex);
- }
- finally {
- resetContextHolders(request, previousLocaleContext, previousAttributes);
- if (requestAttributes != null) {
- requestAttributes.requestCompleted();
- }
- if (logger.isDebugEnabled()) {
- if (failureCause != null) {
- this.logger.debug("Could not complete request", failureCause);
- }
- else {
- if (asyncManager.isConcurrentHandlingStarted()) {
- logger.debug("Leaving response open for concurrent processing");
- }
- else {
- this.logger.debug("Successfully completed request");
- }
- }
- }
- publishRequestHandledEvent(request, response, startTime, failureCause);
- }
- }
- /**
- * Build a LocaleContext for the given request, exposing the request's primary locale as current locale.
- * <p>The default implementation uses the dispatcher's LocaleResolver to obtain the current locale,
- * which might change during a request.
- * @param request current HTTP request
- * @return the corresponding LocaleContext
- */
- @Override
- protected LocaleContext buildLocaleContext(final HttpServletRequest request) {
- if (this.localeResolver instanceof LocaleContextResolver) {
- return ((LocaleContextResolver) this.localeResolver).resolveLocaleContext(request);
- }
- else {
- return new LocaleContext() {
- @Override
- public Locale getLocale() {
- return localeResolver.resolveLocale(request);
- }
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement