Skip to content

Commit 4de6b2e

Browse files
committed
feat: handle incomplete response scenarios with appropriate error messages
1 parent 387f3eb commit 4de6b2e

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

src/main/java/com/laker/postman/service/http/okhttp/OkHttpResponseHandler.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,18 @@ private static void handleBinaryResponse(Response okResponse, HttpResponse respo
293293
return;
294294
}
295295
if (is != null) {
296-
FileAndSize fs = saveInputStreamToTempFile(is, "easyPostman_download_", null, contentLengthHeader);
297-
response.filePath = fs.file.getAbsolutePath();
298-
response.body = I18nUtil.getMessage(MessageKeys.BINARY_SAVED_TEMP_FILE);
299-
response.bodySize = fs.size;
296+
try {
297+
FileAndSize fs = saveInputStreamToTempFile(is, "easyPostman_download_", null, contentLengthHeader);
298+
response.filePath = fs.file.getAbsolutePath();
299+
response.body = I18nUtil.getMessage(MessageKeys.BINARY_SAVED_TEMP_FILE);
300+
response.bodySize = fs.size;
301+
} catch (EOFException e) {
302+
// 处理下载过程中连接中断的情况
303+
log.error("Failed to download complete binary response: {}", e.getMessage());
304+
response.body = I18nUtil.getMessage(MessageKeys.RESPONSE_INCOMPLETE, e.getMessage());
305+
response.bodySize = 0;
306+
response.filePath = null;
307+
}
300308
} else {
301309
response.body = I18nUtil.getMessage(MessageKeys.NO_RESPONSE_BODY);
302310
response.bodySize = 0;
@@ -335,7 +343,21 @@ private static void handleTextResponse(Response okResponse, HttpResponse respons
335343
return;
336344
}
337345
if (body != null) {
338-
byte[] bytes = body.bytes();
346+
byte[] bytes;
347+
try {
348+
bytes = body.bytes();
349+
} catch (EOFException e) {
350+
// 处理响应体不完整的情况(网络中断、服务器过早关闭连接等)
351+
log.error("Failed to read complete response body: {}", e.getMessage());
352+
response.body = I18nUtil.getMessage(MessageKeys.RESPONSE_INCOMPLETE, e.getMessage());
353+
response.bodySize = 0;
354+
response.filePath = null;
355+
return;
356+
} catch (IOException e) {
357+
// 处理其他 IO 异常
358+
log.error("Error reading response body: {}", e.getMessage(), e);
359+
throw e;
360+
}
339361
response.bodySize = bytes.length;
340362
if (bytes.length > getMaxBodySize()) { // 如果解压后内容超过设置值,保存为临时文件
341363
String extension = ext != null ? ext : ".txt";

src/main/java/com/laker/postman/util/MessageKeys.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ public final class MessageKeys {
796796
public static final String TEXT_TOO_LARGE_BODY = "text.too_large.body";
797797
public static final String BODY_TOO_LARGE_SAVED = "body.too_large.saved";
798798
public static final String SSE_STREAM_UNSUPPORTED = "sse.stream.unsupported";
799+
public static final String RESPONSE_INCOMPLETE = "response.incomplete";
799800

800801
// ============ ResponseAssertion 国际化 ============
801802
public static final String RESPONSE_ASSERTION_STATUS_FAILED = "response.assertion.status_failed";

src/main/resources/messages_en.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ text.too_large=Text content size {0}MB exceeds the max download limit ({1} MB)
681681
text.too_large.body=[Text content exceeds max download limit, not downloaded. Limit: {0} MB]
682682
body.too_large.saved=[Body exceeds {0}KB, saved as temp file. Download to view full content]
683683
sse.stream.unsupported=[SSE stream response, cannot be handled directly]
684+
response.incomplete=[Incomplete response: connection closed prematurely. Error: {0}]
684685
# ResponseAssertion i18n
685686
response.assertion.status_failed=Status code assertion failed: expected={0}, actual={1}
686687
response.assertion.header_not_found=Response header not found

src/main/resources/messages_zh.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ text.too_large=文本内容大小 {0}MB 超出最大下载限制({1} MB)
679679
text.too_large.body=[文本内容超出最大下载限制,未下载。限制:{0} MB]
680680
body.too_large.saved=[响应体内容超过{0}KB,已保存为临时文件,可下载查看完整内容]
681681
sse.stream.unsupported=[SSE 流响应,无法直接处理]
682+
response.incomplete=[响应不完整:连接过早关闭。错误:{0}]
682683
# ResponseAssertion 国际化
683684
response.assertion.status_failed=响应状态码断言失败: 期望={0}, 实际={1}
684685
response.assertion.header_not_found=响应头不存在

0 commit comments

Comments
 (0)