I am writing an exception handler for a WebClient response in Spring Webflux. I haven't included the code before and after my broken snippet to keep this short and sweet. Currently I have the following code:
.retrieve()
.bodyToMono(String::class.java)
.doOnError {
WebClientResponseException::class.java,
error -> logger.error(error.getResponseBodyAsString())
}
.awaitSingle()
I am new to Kotlin and the Spring Webflux's reactive programming. I can't understand what IntelliJ means by
Unexpected tokens (use ';' to separate expressions on the same line)
The syntax looks correct to me for the following method signature:
<E extends Throwable> Mono<T> doOnError(Class<E> exceptionType, final Consumer<? super E> one rror)
CodePudding user response:
It's a little difficult to answer your question, because it seems that the doOnError function your IDE is displaying is different from the one you've posted documentation for. After all, the IDE shows that the lambda is valid and has a single Throwable parameter, but the documentation you've posted requires two arguments.
If you just want to name the lambda paramater, you must do that right at the very beginning of the lambda:
.doOnError { error ->
// your code here
}
If you need to specify the first argument, you do so inside parentheses like you would for a normal method:
.doOnError(WebClientResponseException::class.java) { error ->
logger.error(error.getResponseBodyAsString())
}

