Skip to content

Instantly share code, notes, and snippets.

@jiraguha
Created April 17, 2020 20:18
Show Gist options
  • Save jiraguha/9df52bc2149a5cae0320a3776dbbc772 to your computer and use it in GitHub Desktop.
Save jiraguha/9df52bc2149a5cae0320a3776dbbc772 to your computer and use it in GitHub Desktop.
Try have exception advising in spring for RouterFunction (webflux)
@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