Class DefaultErrorWebExceptionHandler
- All Implemented Interfaces:
InitializingBean, ErrorWebExceptionHandler, WebExceptionHandler
WebExceptionHandler, rendering
ErrorAttributes.
More specific errors can be handled either using Spring WebFlux abstractions (e.g.
@ExceptionHandler with the annotation model) or by adding
RouterFunction to the chain.
This implementation will render error as HTML views if the client explicitly supports
that media type. It attempts to resolve error views using well known conventions. Will
search for templates and static assets under '/error' using the
status code and the status series.
For example, an HTTP 404 will search (in the specific order):
'/<templates>/error/404.<ext>''/<static>/error/404.html''/<templates>/error/4xx.<ext>''/<static>/error/4xx.html''/<templates>/error/error''/<static>/error/error.html'
If none found, a default "Whitelabel Error" HTML view will be rendered.
If the client doesn't support HTML, the error information will be rendered as a JSON payload.
- Since:
- 4.0.0
- Author:
- Brian Clozel, Scott Frederick, Moritz Halbritter
-
Constructor Summary
ConstructorsConstructorDescriptionDefaultErrorWebExceptionHandler(ErrorAttributes errorAttributes, WebProperties.Resources resources, ErrorProperties errorProperties, ApplicationContext applicationContext) Create a newDefaultErrorWebExceptionHandlerinstance. -
Method Summary
Modifier and TypeMethodDescriptionprotected RequestPredicatePredicate that checks whether the current request explicitly support"text/html"media type.protected ErrorAttributeOptionsgetErrorAttributeOptions(ServerRequest request, MediaType mediaType) protected intgetHttpStatus(Map<String, @Nullable Object> errorAttributes) Get the HTTP error status information from the error map.protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) Create aRouterFunctionthat can route and handle errors as JSON responses or HTML views.protected booleanisIncludeBindingErrors(ServerRequest request, MediaType produces) Determine if the errors attribute should be included.protected booleanisIncludeMessage(ServerRequest request, MediaType produces) Determine if the message attribute should be included.protected booleanisIncludePath(ServerRequest request, MediaType produces) Determine if the path attribute should be included.protected booleanisIncludeStackTrace(ServerRequest request, MediaType produces) Determine if the stacktrace attribute should be included.protected reactor.core.publisher.Mono<ServerResponse> renderErrorResponse(ServerRequest request) Render the error information as a JSON payload.protected reactor.core.publisher.Mono<ServerResponse> renderErrorView(ServerRequest request) Render the error information as an HTML view.
-
Constructor Details
-
DefaultErrorWebExceptionHandler
public DefaultErrorWebExceptionHandler(ErrorAttributes errorAttributes, WebProperties.Resources resources, ErrorProperties errorProperties, ApplicationContext applicationContext) Create a newDefaultErrorWebExceptionHandlerinstance.- Parameters:
errorAttributes- the error attributesresources- the resources configuration propertieserrorProperties- the error configuration propertiesapplicationContext- the current application context
-
-
Method Details
-
getRoutingFunction
Description copied from class:AbstractErrorWebExceptionHandlerCreate aRouterFunctionthat can route and handle errors as JSON responses or HTML views.If the returned
RouterFunctiondoesn't route to aHandlerFunction, the original exception is propagated in the pipeline and can be processed by otherWebExceptionHandlers.- Specified by:
getRoutingFunctionin classAbstractErrorWebExceptionHandler- Parameters:
errorAttributes- theErrorAttributesinstance to use to extract error information- Returns:
- a
RouterFunctionthat routes and handles errors
-
renderErrorView
Render the error information as an HTML view.- Parameters:
request- the current request- Returns:
- a
Publisherof the HTTP response
-
renderErrorResponse
Render the error information as a JSON payload.- Parameters:
request- the current request- Returns:
- a
Publisherof the HTTP response
-
getErrorAttributeOptions
protected ErrorAttributeOptions getErrorAttributeOptions(ServerRequest request, MediaType mediaType) -
isIncludeStackTrace
Determine if the stacktrace attribute should be included.- Parameters:
request- the source requestproduces- the media type produced (orMediaType.ALL)- Returns:
- if the stacktrace attribute should be included
-
isIncludeMessage
Determine if the message attribute should be included.- Parameters:
request- the source requestproduces- the media type produced (orMediaType.ALL)- Returns:
- if the message attribute should be included
-
isIncludeBindingErrors
Determine if the errors attribute should be included.- Parameters:
request- the source requestproduces- the media type produced (orMediaType.ALL)- Returns:
- if the errors attribute should be included
-
isIncludePath
Determine if the path attribute should be included.- Parameters:
request- the source requestproduces- the media type produced (orMediaType.ALL)- Returns:
- if the path attribute should be included
-
getHttpStatus
-
acceptsTextHtml
Predicate that checks whether the current request explicitly support"text/html"media type.The "match-all" media type is not considered here.
- Returns:
- the request predicate
-