public class WebErrorHandlers extends Object
WebErrorHandler implementations. The factory would query all the
implementations to find an appropriate exception handler to handle a particular exception.
WebErrorHandlers is also responsible for Error Code to Error Message translation
and i18n (RIP SRP!). In order to fulfill this requirement, we need an instance of MessageSource
to translate error codes to error messages.
WebErrorHandler implementation to handle the
exception, we would use the LastResortWebErrorHandler as the default exception handler.
If you don't like its exception handling approach, consider passing a valid non-null
defaultWebErrorHandler to the constructor.WebErrorHandler,
HandledException,
LastResortWebErrorHandler| Constructor and Description |
|---|
WebErrorHandlers(org.springframework.context.MessageSource messageSource,
List<WebErrorHandler> implementations,
WebErrorHandler defaultWebErrorHandler,
ExceptionRefiner exceptionRefiner)
To initialize the
WebErrorHandlers instance with a code-to-message translator, a
non-empty collection of WebErrorHandler implementations and an optional fallback
error handler. |
| Modifier and Type | Method and Description |
|---|---|
HttpError |
handle(Throwable exception,
Locale locale)
Given any
exception, first it would select an appropriate exception handler or
falls back to a default handler and then tries to handle the exception using the chosen
handler. |
public WebErrorHandlers(@NonNull
org.springframework.context.MessageSource messageSource,
@NonNull
List<WebErrorHandler> implementations,
@Nullable
WebErrorHandler defaultWebErrorHandler,
@Nullable
ExceptionRefiner exceptionRefiner)
WebErrorHandlers instance with a code-to-message translator, a
non-empty collection of WebErrorHandler implementations and an optional fallback
error handler.messageSource - The code to message translator.implementations - Collection of WebErrorHandler implementations.defaultWebErrorHandler - Fallback web error handler.exceptionRefiner - Possibly can refine exceptions before handling them.NullPointerException - When one of the required parameters is null.IllegalArgumentException - When the collection of implementations is empty.@NonNull public HttpError handle(@Nullable Throwable exception, @Nullable Locale locale)
exception, first it would select an appropriate exception handler or
falls back to a default handler and then tries to handle the exception using the chosen
handler. Then would convert the HandledException to its corresponding HttpError.exception - The exception to handle.locale - Will be used to target a specific locale while translating the codes to error
messages.HttpError instance containing both error and message combinations and also,
the intended HTTP Status Code.Copyright © 2018. All rights reserved.