-
Notifications
You must be signed in to change notification settings - Fork 40.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No error body when using "functional Handlers" and Spring MVC during tests #40558
Comments
functional Handlers
and Spring MVC during tests
This is due to a difference in how errors are handled by WebFlux vs how they're handled by the servlet spec. The latter has the concept of error pages, with which Spring Boot integrates, that rely on the request being forwarded to the correct error page. |
For others reading this thread, I used this code: class MockMvcRestExceptionConfiguration(
private val errorController: BasicErrorController,
private val om: ObjectMapper
) : WebMvcConfigurer {
override fun addInterceptors(registry: InterceptorRegistry) {
registry.addInterceptor(object : HandlerInterceptor {
@Throws(Exception::class)
override fun afterCompletion(
request: HttpServletRequest,
response: HttpServletResponse,
handler: Any,
@Nullable ex: java.lang.Exception?
) {
val status: Int = response.status
if (status < 400) return
request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE, status)
request.setAttribute(WebUtils.ERROR_STATUS_CODE_ATTRIBUTE, status)
request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, request.requestURI.toString())
// The original exception is already saved as an attribute request
when (val exception = request.getAttribute(DispatcherServlet.EXCEPTION_ATTRIBUTE) as Exception?) {
null -> {}
is ResponseStatusException -> request.apply {
setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, exception)
setAttribute(WebUtils.ERROR_MESSAGE_ATTRIBUTE, exception.reason)
}
}
om.writeValue(response.outputStream, errorController.error(request).body)
}
})
}
} @WebMvcTest(controllers = […])
@Import(…, MockMvcRestExceptionConfiguration::class)
class ItemHandlerTest(…) { … } |
Hello Spring team 👋 ,
I continue my work on Spring MVC + functional endpoints (as part of a migration from webflux to standard), I think I've found another limitation compared to the WebFlux version.
I would like to test a very simple application:
However, I found no way to get the "body" out of the error during test. With the following code, the value
v
is empty.The strange part is the
BasicErrorController
is instantiated during test phase, but I don't know whyerror(HttpServletRequest request)
is not called.Of course, this works perfectly fine in "prod", when the app is run locally:
It's important to note I use the same pattern (with
@ImportAutoConfiguration(ErrorWebFluxAutoConfiguration::class)
) for many years successfully, so I was surprised to hit such a case withMVC
+ functionnal endpoints.The code is available in this repository
Thank you for your help and support 👋
The text was updated successfully, but these errors were encountered: