Class AbstractErrorWebExceptionHandler

java.lang.Object
org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler
All Implemented Interfaces:
InitializingBean, ErrorWebExceptionHandler, WebExceptionHandler
Direct Known Subclasses:
DefaultErrorWebExceptionHandler

public abstract class AbstractErrorWebExceptionHandler extends Object implements ErrorWebExceptionHandler, InitializingBean
Abstract base class for ErrorWebExceptionHandler implementations.
Since:
2.0.0
Author:
Brian Clozel, Scott Frederick, Moritz Halbritter
See Also:
  • Constructor Details

    • AbstractErrorWebExceptionHandler

      public AbstractErrorWebExceptionHandler(ErrorAttributes errorAttributes, WebProperties.Resources resources, ApplicationContext applicationContext)
      Create a new AbstractErrorWebExceptionHandler.
      Parameters:
      errorAttributes - the error attributes
      resources - the resources configuration properties
      applicationContext - the application context
      Since:
      2.4.0
  • Method Details

    • setMessageWriters

      public void setMessageWriters(List<HttpMessageWriter<?>> messageWriters)
      Configure HTTP message writers to serialize the response body with.
      Parameters:
      messageWriters - the HttpMessageWriters to use
    • setMessageReaders

      public void setMessageReaders(List<HttpMessageReader<?>> messageReaders)
      Configure HTTP message readers to deserialize the request body with.
      Parameters:
      messageReaders - the HttpMessageReaders to use
    • setViewResolvers

      public void setViewResolvers(List<ViewResolver> viewResolvers)
      Configure the ViewResolver to use for rendering views.
      Parameters:
      viewResolvers - the list of ViewResolvers to use
    • getErrorAttributes

      protected Map<String,Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options)
      Extract the error attributes from the current request, to be used to populate error views or JSON payloads.
      Parameters:
      request - the source request
      options - options to control error attributes
      Returns:
      the error attributes as a Map
    • getError

      protected Throwable getError(ServerRequest request)
      Extract the original error from the current request.
      Parameters:
      request - the source request
      Returns:
      the error
    • isTraceEnabled

      protected boolean isTraceEnabled(ServerRequest request)
      Check whether the trace attribute has been set on the given request.
      Parameters:
      request - the source request
      Returns:
      true if the error trace has been requested, false otherwise
    • isMessageEnabled

      protected boolean isMessageEnabled(ServerRequest request)
      Check whether the message attribute has been set on the given request.
      Parameters:
      request - the source request
      Returns:
      true if the message attribute has been requested, false otherwise
    • isBindingErrorsEnabled

      protected boolean isBindingErrorsEnabled(ServerRequest request)
      Check whether the errors attribute has been set on the given request.
      Parameters:
      request - the source request
      Returns:
      true if the errors attribute has been requested, false otherwise
    • isPathEnabled

      protected boolean isPathEnabled(ServerRequest request)
      Check whether the path attribute has been set on the given request.
      Parameters:
      request - the source request
      Returns:
      true if the path attribute has been requested, false otherwise
      Since:
      3.3.0
    • renderErrorView

      protected reactor.core.publisher.Mono<ServerResponse> renderErrorView(String viewName, ServerResponse.BodyBuilder responseBody, Map<String,Object> error)
      Render the given error data as a view, using a template view if available or a static HTML file if available otherwise. This will return an empty Publisher if none of the above are available.
      Parameters:
      viewName - the view name
      responseBody - the error response being built
      error - the error data as a map
      Returns:
      a Publisher of the ServerResponse
    • renderDefaultErrorView

      protected reactor.core.publisher.Mono<ServerResponse> renderDefaultErrorView(ServerResponse.BodyBuilder responseBody, Map<String,Object> error)
      Render a default HTML "Whitelabel Error Page".

      Useful when no other error view is available in the application.

      Parameters:
      responseBody - the error response being built
      error - the error data as a map
      Returns:
      a Publisher of the ServerResponse
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Specified by:
      afterPropertiesSet in interface InitializingBean
      Throws:
      Exception
    • getRoutingFunction

      protected abstract RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes)
      Create a RouterFunction that can route and handle errors as JSON responses or HTML views.

      If the returned RouterFunction doesn't route to a HandlerFunction, the original exception is propagated in the pipeline and can be processed by other WebExceptionHandlers.

      Parameters:
      errorAttributes - the ErrorAttributes instance to use to extract error information
      Returns:
      a RouterFunction that routes and handles errors
    • handle

      public reactor.core.publisher.Mono<Void> handle(ServerWebExchange exchange, Throwable throwable)
      Specified by:
      handle in interface WebExceptionHandler
    • logError

      protected void logError(ServerRequest request, ServerResponse response, Throwable throwable)
      Logs the throwable error for the given request and response exchange. The default implementation logs all errors at debug level. Additionally, any internal server error (500) is logged at error level.
      Parameters:
      request - the request that was being handled
      response - the response that was being sent
      throwable - the error to be logged
      Since:
      2.2.0