Enhance DefaultResponseErrorHandler to allow logging complete error response body #27552
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
status: backported
An issue that has been backported to maintenance branches
type: enhancement
A general enhancement
Milestone
Affects: 5.2.14.RELEASE
This enhancement request is related to following Stack Overflow question.
https://stackoverflow.com/questions/69475757/how-to-print-complete-error-message-when-logging-an-exception/69527775
Problem Statement:
When using
RestTemplate
to call any REST API and it returns any 4xx or 5xx HTTP error code along with message body, complete message body is not getting logged if we simply catch and log theException
object.Here is a minimum reproducible example:
Output:
You can notice response message is truncated with
"Glo... (593 bytes)]
in above stack trace.Benefits of the enhancement:
Though, we can catch
HttpClientErrorException
and usegetResponseBodyAsString()
to read response body but I think we should be able to configure the size of error message to be logged before truncation kicks in when loggingException
object.RestTemplate
.Root Cause of this issue:
Currently,
DefaultResponseErrorHandler
class has hardcoded size of 200 after which it truncates the error response body.spring-framework/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java
Line 145 in b595dc1
Rather than the consumers writing custom response error handlers, it would be much better if this hardcoded
maxChars
size can be made configurable.The text was updated successfully, but these errors were encountered: