8000 fix: Add error message and update javadoc on DownloadResponse (#21449) · vaadin/flow@9a6a820 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 9a6a820

Browse files
authored
fix: Add error message and update javadoc on DownloadResponse (#21449)
Add possibility to give an error message to the DownloadResponse. Add information on stream handling for DownloadResponse so it is clear that the one using the response is responsible for closing stream.
1 parent 9dae733 commit 9a6a820

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

flow-server/src/main/java/com/vaadin/flow/server/streams/DownloadResponse.java

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
/**
2525
* Data class containing required information for sending the given input stream
2626
* to the client.
27+
* <p>
28+
* The given input stream will be read at a later time and will be automatically
29+
* closed by the caller.
2730
*
2831
* @since 24.8
2932
*/
@@ -36,12 +39,14 @@ public class DownloadResponse implements Serializable {
3639
private final int size;
3740

3841
private Integer error;
42+
private String errorMessage;
3943

4044
/**
4145
* Create a download response with content stream and content data.
4246
*
4347
* @param inputStream
44-
* data stream for data to send to client
48+
* data stream for data to send to client, stream will be closed
49+
* automatically after use by the caller.
4550
* @param fileName
4651
* file name of download
4752
* @param contentType
@@ -59,6 +64,8 @@ public DownloadResponse(InputStream inputStream, String fileName,
5964

6065
/**
6166
* Get the InputStream to read the content data from.
67+
* <p>
68+
* InputStream needs to be closed by the called after reading is over.
6269
*
6370
* @return content InputStream
6471
*/
@@ -107,6 +114,22 @@ public static DownloadResponse error(int statusCode) {
107114
return downloadResponse;
108115
}
109116

117+
/**
118+
* Generate an error response for download with message.
119+
*
120+
* @param statusCode
121+
* error status code
122+
* @param message
123+
* error message for details on what went wrong
124+
* @return DownloadResponse for request
125+
*/
126+
public static DownloadResponse error(int statusCode, String message) {
127+
DownloadResponse downloadResponse = new DownloadResponse(null, null,
128+
null, -1);
129+
downloadResponse.setError(statusCode, message);
130+
return downloadResponse;
131+
}
132+
110133
/**
111134
* Generate an error response for download.
112135
*
@@ -121,6 +144,23 @@ public static DownloadResponse error(HttpStatusCode statusCode) {
121144
return downloadResponse;
122145
}
123146

147+
/**
148+
* Generate an error response for download with message.
149+
*
150+
* @param statusCode
151+
* error status code
152+
* @param message
153+
* error message for details on what went wrong
154+
* @return DownloadResponse for request
155+
*/
156+
public static DownloadResponse error(HttpStatusCode statusCode,
157+
String message) {
158+
DownloadResponse downloadResponse = new DownloadResponse(null, null,
159+
null, -1);
160+
downloadResponse.setError(statusCode, message);
161+
return downloadResponse;
162+
}
163+
124164
/**
125165
* Check if response has an error code.
126166
*
@@ -140,6 +180,19 @@ public void setError(int error) {
140180
this.error = error;
141181
}
142182

183+
/**
184+
* Set http error code and error message.
185+
*
186+
* @param error
187+
* error code
188+
* @param errorMessage
189+
* error message
190+
*/
191+
public< 8000 /span> void setError(int error, String errorMessage) {
192+
this.error = error;
193+
this.errorMessage = errorMessage;
194+
}
195+
143196
/**
144197
* Set http error code.
145198
*
@@ -150,6 +203,19 @@ public void setError(HttpStatusCode error) {
150203
this.error = error.getCode();
151204
}
152205

206+
/**
207+
* Set http error code and error message.
208+
*
209+
* @param error
210+
* error code
211+
* @param errorMessage
212+
* error message
213+
*/
214+
public void setError(HttpStatusCode error, String errorMessage) {
215+
this.error = error.getCode();
216+
this.errorMessage = errorMessage;
217+
}
218+
153219
/**
154220
* Get the set error code.
155221
*
@@ -161,4 +227,13 @@ public int getError() {
161227
}
162228
return error;
163229
}
230+
231+
/**
232+
* Get error message if set for error response.
233+
*
234+
* @return error message or null if not set
235+
*/
236+
public String getErrorMessage() {
237+
return errorMessage;
238+
}
164239
}

0 commit comments

Comments
 (0)
0