/
ResponseStatusException.java
135 lines (118 loc) · 4.24 KB
/
ResponseStatusException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright 2002-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.server;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.lang.Nullable;
import org.springframework.web.ErrorResponseException;
/**
* Subclass of {@link ErrorResponseException} that accepts a "reason", and by
* default maps that to the {@link ErrorResponseException#setDetail(String) "detail"}
* of the {@code ProblemDetail}.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 5.0
*/
@SuppressWarnings("serial")
public class ResponseStatusException extends ErrorResponseException {
@Nullable
private final String reason;
/**
* Constructor with a response status.
* @param status the HTTP status (required)
*/
public ResponseStatusException(HttpStatusCode status) {
this(status, null);
}
/**
* Constructor with a response status and a reason to add to the exception
* message as explanation.
* @param status the HTTP status (required)
* @param reason the associated reason (optional)
*/
public ResponseStatusException(HttpStatusCode status, @Nullable String reason) {
this(status, reason, null);
}
/**
* Constructor with a response status and a reason to add to the exception
* message as explanation, as well as a nested exception.
* @param rawStatusCode the HTTP status code value
* @param reason the associated reason (optional)
* @param cause a nested exception (optional)
* @since 5.3
*/
public ResponseStatusException(int rawStatusCode, @Nullable String reason, @Nullable Throwable cause) {
this(HttpStatusCode.valueOf(rawStatusCode), reason, cause);
}
/**
* Constructor with a response status and a reason to add to the exception
* message as explanation, as well as a nested exception.
* @param status the HTTP status (required)
* @param reason the associated reason (optional)
* @param cause a nested exception (optional)
*/
public ResponseStatusException(HttpStatusCode status, @Nullable String reason, @Nullable Throwable cause) {
this(status, reason, cause, null, null);
}
/**
* Constructor with a message code and arguments for resolving the error
* "detail" via {@link org.springframework.context.MessageSource}.
* @param status the HTTP status (required)
* @param reason the associated reason (optional)
* @param cause a nested exception (optional)
* @since 6.0
*/
protected ResponseStatusException(
HttpStatusCode status, @Nullable String reason, @Nullable Throwable cause,
@Nullable String messageDetailCode, @Nullable Object[] messageDetailArguments) {
super(status, ProblemDetail.forStatus(status), cause, messageDetailCode, messageDetailArguments);
this.reason = reason;
setDetail(reason);
}
/**
* The reason explaining the exception (potentially {@code null} or empty).
*/
@Nullable
public String getReason() {
return this.reason;
}
/**
* Return headers to add to the error response, e.g. "Allow", "Accept", etc.
* <p>By default, delegates to {@link #getResponseHeaders()} for backwards
* compatibility.
*/
@Override
public HttpHeaders getHeaders() {
return getResponseHeaders();
}
/**
* Return headers associated with the exception that should be added to the
* error response, e.g. "Allow", "Accept", etc.
* <p>The default implementation in this class returns empty headers.
* @since 5.1.13
* @deprecated as of 6.0 in favor of {@link #getHeaders()}
*/
@Deprecated(since = "6.0")
public HttpHeaders getResponseHeaders() {
return HttpHeaders.EMPTY;
}
@Override
public String getMessage() {
return getStatusCode() + (this.reason != null ? " \"" + this.reason + "\"" : "");
}
}