mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2025-12-17 12:53:02 +00:00
json异常时, 快速失败
This commit is contained in:
@@ -168,9 +168,9 @@ public abstract class PanBase implements IPanTool {
|
|||||||
* @return JsonObject
|
* @return JsonObject
|
||||||
*/
|
*/
|
||||||
protected JsonObject asJson(HttpResponse<?> res) {
|
protected JsonObject asJson(HttpResponse<?> res) {
|
||||||
try {
|
|
||||||
// 检查响应头中的Content-Encoding是否为gzip
|
// 检查响应头中的Content-Encoding是否为gzip
|
||||||
String contentEncoding = res.getHeader("Content-Encoding");
|
String contentEncoding = res.getHeader("Content-Encoding");
|
||||||
|
try {
|
||||||
if ("gzip".equalsIgnoreCase(contentEncoding)) {
|
if ("gzip".equalsIgnoreCase(contentEncoding)) {
|
||||||
// 如果是gzip压缩的响应体,解压
|
// 如果是gzip压缩的响应体,解压
|
||||||
return new JsonObject(decompressGzip((Buffer) res.body()));
|
return new JsonObject(decompressGzip((Buffer) res.body()));
|
||||||
@@ -179,10 +179,45 @@ public abstract class PanBase implements IPanTool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
if ("gzip".equalsIgnoreCase(contentEncoding)) {
|
||||||
|
// 如果是gzip压缩的响应体,解压
|
||||||
|
try {
|
||||||
|
log.error(decompressGzip((Buffer) res.body()));
|
||||||
|
fail(decompressGzip((Buffer) res.body()));
|
||||||
|
throw new RuntimeException("响应不是JSON格式");
|
||||||
|
} catch (IOException ex) {
|
||||||
|
log.error("响应gzip解压失败");
|
||||||
|
fail("响应gzip解压失败: {}", ex.getMessage());
|
||||||
|
throw new RuntimeException("响应gzip解压失败", ex);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.error("解析失败: json格式异常: {}", res.bodyAsString());
|
||||||
fail("解析失败: json格式异常: {}", res.bodyAsString());
|
fail("解析失败: json格式异常: {}", res.bodyAsString());
|
||||||
throw new RuntimeException("解析失败: json格式异常");
|
throw new RuntimeException("解析失败: json格式异常");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* body To text的封装, 会自动处理异常, 会自动解压gzip
|
||||||
|
* @param res HttpResponse
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
protected String asText(HttpResponse<?> res) {
|
||||||
|
// 检查响应头中的Content-Encoding是否为gzip
|
||||||
|
String contentEncoding = res.getHeader("Content-Encoding");
|
||||||
|
try {
|
||||||
|
if ("gzip".equalsIgnoreCase(contentEncoding)) {
|
||||||
|
// 如果是gzip压缩的响应体,解压
|
||||||
|
return decompressGzip((Buffer) res.body());
|
||||||
|
} else {
|
||||||
|
return res.bodyAsString();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("解析失败: res格式异常");
|
||||||
|
throw new RuntimeException("解析失败: res格式异常");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected void complete(String url) {
|
protected void complete(String url) {
|
||||||
promise.complete(url);
|
promise.complete(url);
|
||||||
|
|||||||
Reference in New Issue
Block a user