Hi all, I’m using springboot 3.2.0 along with the ...
# spring
k
Hi all, I’m using springboot 3.2.0 along with the latest version of webflux. I’m receiving the following error on the following code and its driving me insane. What I’m trying to achieve is to catch my own exception that I throw within controllers so that I can return some generic response. Any ideas?:
Copy code
@ControllerAdvice
class ErrorExceptionHandler {
  private val logger: Logger = LoggerFactory.getLogger(this::class.java)

  @ExceptionHandler(ApplicationException::class)
  suspend fun onApplicationException(exception: ApplicationException, request: WebRequest): ResponseEntity<String> {
    logger.warn("We are in the exception handler!")
    return ResponseEntity.badRequest().body("bad!")
  }
}
Copy code
DEBUG [TestApp,173201bb4ca1afc45467a02904f0bc24,85bbccda6da914f1] 35257  --- [nio-8080-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Using @ExceptionHandler com.testapp.configurations.http.ErrorExceptionHandler#onApplicationException(ApplicationException, WebRequest, Continuation)
WARN [TestApp,173201bb4ca1afc45467a02904f0bc24,85bbccda6da914f1] 35257  --- [nio-8080-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Failure in @ExceptionHandler com.testapp.configurations.http.ErrorExceptionHandler#onApplicationException(ApplicationException, WebRequest, Continuation)
Copy code
java.lang.IllegalStateException: Could not resolve parameter [2] in public java.lang.Object com.testapp.configurations.http.ErrorExceptionHandler.onApplicationException(com.testapp.ApplicationException,org.springframework.web.context.request.WebRequest,kotlin.coroutines.Continuation<? super org.springframework.http.ResponseEntity<java.lang.String>>): No suitable resolver	
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:223)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:179)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118)
	at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:432)
	at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:74)
	at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:175)
	at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:80)
    ...
r
could it be the
suspend
modifier in your handler? It adds a
Continuation
to the generated method, and may be causing your issue
k
I’ve tried it without the suspend modifier and it still kicks the bucket
r
Just replicated it here: The
suspend
modifier is indeed fine. The signature of
onApplicationException
for webflux does not have the request as a parameter. You should use it as:
Copy code
suspend fun onApplicationException(exception: ApplicationException): ResponseEntity<String> {
    logger.warn("We are in the exception handler!")
    return ResponseEntity.badRequest().body("bad!")
  }
k
Thanks @renatomrcosta
✌🏽 1
k
in case you need some parts of the request/response, you can add this parameter
Copy code
exchange: ServerWebExchange
👍 1
💡 1
k
Thanks @kubele
🙌🏻 1
h
@Kev WebRequest is available in classic servlet stack.
K 1