Created
April 17, 2020 20:18
-
-
Save jiraguha/9df52bc2149a5cae0320a3776dbbc772 to your computer and use it in GitHub Desktop.
Try have exception advising in spring for RouterFunction (webflux)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
@Order(-2) | |
class GlobalErrorWebExceptionHandler( | |
private val configurer: ServerCodecConfigurer, | |
private val exceptionAdvisor: ExceptionAdvisor, | |
private val errorAttributes: ErrorAttributes, | |
private val resourceProperties: ResourceProperties, | |
private val applicationContext: ApplicationContext) | |
: AbstractErrorWebExceptionHandler(errorAttributes, resourceProperties, applicationContext) { | |
init { | |
this.setMessageWriters(configurer.writers) | |
} | |
override fun getRoutingFunction(errorAttributes: ErrorAttributes): RouterFunction<ServerResponse> { | |
return RouterFunctions.route(RequestPredicates.all(), HandlerFunction { | |
request: ServerRequest -> renderErrorResponse(request) | |
}) | |
} | |
private fun renderErrorResponse(request: ServerRequest): Mono<ServerResponse> { | |
return when (val throwable = getError(request)) { | |
is Problem -> exceptionAdvisor.ProblemHandler(throwable, request.exchange()) | |
else -> exceptionAdvisor.handleThrowable(throwable, request.exchange()) | |
}.flatMap { it.toServerResponse() } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment