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

HttpStatusCodeException - remove HttpStatusCode from message details #34209

Closed
membersound opened this issue Jan 7, 2025 · 3 comments
Closed
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@membersound
Copy link

membersound commented Jan 7, 2025

In terms of DRY (don't repeat yourself), I often feel that Spring adding the StatusCode in front of a HttpStatusCodeException is somehow counter-intuitive. And especially is against DRY, because the statuscode is anyway offered as http status code in the response.

Thus, I want to suggest the following test should succeed:

@Test
public void test() {
   assertEquals("junit", new HttpStatusCodeException(HttpStatusCode.FORBIDDEN, "junit"); //fails, because message = "403 junit"
}

Because as of now, if the HttpStatusCodeException is thrown from a @RestController, Spring renders a message like:

{ 
 ...
 "message": "403 junit"
 ...
}

Source:
HttpStatusCodeException.class:

	private static String getMessage(HttpStatusCode statusCode, String statusText) {
		if (!StringUtils.hasLength(statusText) && statusCode instanceof HttpStatus status) {
			statusText = status.getReasonPhrase();
		}
		return statusCode.value() + " " + statusText; //suggestion: remove the statusCode prefix here
	}
``
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 7, 2025
@snicoll
Copy link
Member

snicoll commented Jan 8, 2025

I am not sure what the argument for DRY is. This is an exception that carry a status code, of course its message should output the status code.

Thus, I want to suggest the following test should succeed:

Sorry, but I really see no reason that this should be. This completely ignores the first argument of the exception.

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Jan 8, 2025
@snicoll snicoll added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 8, 2025
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Jan 8, 2025
@membersound
Copy link
Author

Okay, but which exception then has to be thrown in Spring that creates a certain http statuscode in the response, and only carries the custom message? Without prefixing the message?

@snicoll
Copy link
Member

snicoll commented Jan 8, 2025

which exception then has to be thrown in Spring

I am not sure what you're asking. We surely don't provide every exceptions with every possible use cases you may fancy. If you want an exception to behave like that, create your own.

Without prefixing the message?

It isn't the message. it is the statusText (please review the Javadoc).

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) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

4 participants