-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ResponseStatusException associated headers
A ResponseStatus exception now exposes extra method to return headers for the response. This is used in ResponseStatusExceptionHandler to apply the headers to the response. Closes gh-23741
- Loading branch information
1 parent
cc84533
commit 614c7b0
Showing
5 changed files
with
95 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2002-2018 the original author or authors. | ||
* Copyright 2002-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -22,6 +22,7 @@ | |
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.server.reactive.ServerHttpRequest; | ||
import org.springframework.http.server.reactive.ServerHttpResponse; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.web.server.ResponseStatusException; | ||
import org.springframework.web.server.ServerWebExchange; | ||
|
@@ -62,8 +63,7 @@ public void setWarnLogCategory(String loggerName) { | |
|
||
@Override | ||
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) { | ||
HttpStatus status = resolveStatus(ex); | ||
if (status == null || !exchange.getResponse().setStatusCode(status)) { | ||
if (!updateResponse(exchange.getResponse(), ex)) { | ||
return Mono.error(ex); | ||
} | ||
|
||
|
@@ -86,16 +86,25 @@ private String formatError(Throwable ex, ServerHttpRequest request) { | |
return "Resolved [" + reason + "] for HTTP " + request.getMethod() + " " + path; | ||
} | ||
|
||
@Nullable | ||
private HttpStatus resolveStatus(Throwable ex) { | ||
private boolean updateResponse(ServerHttpResponse response, Throwable ex) { | ||
boolean result = false; | ||
HttpStatus status = determineStatus(ex); | ||
if (status == null) { | ||
if (status != null) { | ||
if (response.setStatusCode(status)) { | ||
if (ex instanceof ResponseStatusException) { | ||
((ResponseStatusException) ex).getHeaders() | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
rstoyanchev
Author
Contributor
|
||
.forEach((name, value) -> response.getHeaders().add(name, value)); | ||
} | ||
result = true; | ||
} | ||
} | ||
else { | ||
Throwable cause = ex.getCause(); | ||
if (cause != null) { | ||
status = resolveStatus(cause); | ||
result = updateResponse(response, cause); | ||
} | ||
} | ||
return status; | ||
return result; | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello everyone! Faced with problem: new mech cant transfer multivalues headers like "WWW-Authenticate: Negotiate, NTLM, etc" in raw-style (in a Map<String, List> in this case). ResponseStatusException.getHeaders returns simple Map. I can transfer multivalues only by converting to delimeted string and from it in exception handler. ServerHttpResponse.getHeaders() is HttpHeaders that implements MultiValueMap. Are any restrictions here for using MultiValueMap in ResponseStatusException.getHeaders() instead of Map? Thanks!