Skip to content
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

URL and HTTP method in rest client exception message #26480

Conversation

jerzykrlk
Copy link

Dear team,

This is a followup of a discussion in a past PR: #1956

The rest client exception contains a preview of the body, which is great, and it helps a lot to quickly diagnose errors.

Now, the stacktrace looks like this:

Caused by: org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Server Error: [no body]
	at org.springframework.web.client.HttpServerErrorException.create(HttpServerErrorException.java:101)
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:238)
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:147)

I'd like to add the HTTP method and URL to the error, so that the stacktrace looks like this:

Caused by: org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Server Error after GET http://localhost:49709/status/server : [no body]
	at org.springframework.web.client.HttpServerErrorException.create(HttpServerErrorException.java:101)
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:238)
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:147)

Thank you!

@@ -132,9 +154,10 @@ public void handleError(ClientHttpResponse response) throws IOException {
* </pre>
*/
private String getErrorMessage(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, I'd make this method protected - so that I can override it to extract some more specific information from responses that return HTML, XML or some other nonstandard data.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering something beyond 5.3.x - what about this one?

I'd love to have an extension point where I can further customize the response body preview - format it right, remove some tags, etc.

A long time ago, here: https://github.com/spring-projects/spring-framework/pull/1956/files - I even thought about a separate class to extract the body. But a protected method would be just fine.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 31, 2021
Copy link
Contributor

@rstoyanchev rstoyanchev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the change of protected methods I'd rather avoid this change at this stage of the 5.3.x branch. As far as I can see you can still make it happen in your own custom extension of DefaultResponseErrorHandler, right? It just wouldn't be out of the box.

@@ -175,11 +207,29 @@ private String getErrorMessage(
* @see HttpServerErrorException#create
*/
protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
handleError(null, null, response, statusCode);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a protected method that is now bypassed. Something to consider for a minor/major release but not at this stage of the 5.3.x branch.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into that!

Initially, I didn't think of squeezing it into 5.3. I'd be very happy to do so - but I think the previous changes broke something in spring-integration, so it might not be that easy.

I tried to not to change the behaviour of this method at all. I can also try to rewrite this to make it more 5.3-compatible. But If you think it makes more sense to wait - let's wait.

@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) status: waiting-for-feedback We need additional information before we can continue type: enhancement A general enhancement labels Feb 9, 2021
@jerzykrlk
Copy link
Author

As far as I can see you can still make it happen in your own custom extension of DefaultResponseErrorHandler, right? It just wouldn't be out of the box.

I'm not sure if you're refering to making getErrorMessage protected, or the entire PR?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 9, 2021
throw new UnknownHttpStatusCodeException(message,
response.getRawStatusCode(), response.getStatusText(),
response.getHeaders(), body, getCharset(response));
}
handleError(response, statusCode);
handleError(url, method, response, statusCode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the protected method that's now bypassed.

Copy link
Author

@jerzykrlk jerzykrlk Feb 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation!

Then it can definitely wait for a future Spring release. I'm not in a particular hurry - any custom extensions would well enough.

Should I close the PR, or just keep it here?

Also - is it something that you'd see easy to port into WebClient? Would it make sense to make these two in sync?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can close it for now. On the WebClient side it looks we do have the URL and HTTP method in WebClientResponseException.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback @rstoyanchev !

Is there a chance to squeeze this in a future Spring Framework release? Should I come back once it's announced?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, of course. We can probably re-open this PR then.

@rstoyanchev
Copy link
Contributor

As far as I can see you can still make it happen in your own custom extension of DefaultResponseErrorHandler, right? It just >> wouldn't be out of the box.

I'm not sure if you're refering to making getErrorMessage protected, or the entire PR?

I'm talking about the call to the protected handleError method that is now bypassed.

@rstoyanchev rstoyanchev removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants