From c64320ad784a100626b1e3adaf2059b5ea30ff9f Mon Sep 17 00:00:00 2001 From: LamGC Date: Sat, 13 Jun 2020 16:44:52 +0800 Subject: [PATCH] =?UTF-8?q?[Change]=20HttpRequestException=20=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E6=8E=A5=E5=8F=A3=E5=BC=82=E5=B8=B8,=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E7=B1=BB=E6=89=80=E5=9C=A8=E5=8C=85=E8=B7=AF=E5=BE=84?= =?UTF-8?q?;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/net/lamgc/cgj/bot/cache/CacheStoreCentral.java | 3 ++- .../java/net/lamgc/cgj/bot/cache/ImageCacheHandler.java | 2 +- .../java/net/lamgc/cgj/bot/cache/ImageCacheStore.java | 2 +- .../{bot/cache => }/exception/HttpRequestException.java | 2 +- src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java | 9 +++++---- .../java/net/lamgc/cgj/pixiv/PixivUgoiraBuilder.java | 5 +++-- 6 files changed, 13 insertions(+), 10 deletions(-) rename src/main/java/net/lamgc/cgj/{bot/cache => }/exception/HttpRequestException.java (96%) diff --git a/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreCentral.java b/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreCentral.java index bf85b38..555cb75 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreCentral.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreCentral.java @@ -8,6 +8,7 @@ import net.lamgc.cgj.bot.BotCode; import net.lamgc.cgj.bot.BotCommandProcess; import net.lamgc.cgj.bot.SettingProperties; import net.lamgc.cgj.bot.boot.BotGlobal; +import net.lamgc.cgj.exception.HttpRequestException; import net.lamgc.cgj.pixiv.PixivDownload; import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder; import net.lamgc.cgj.pixiv.PixivURL; @@ -446,7 +447,7 @@ public final class CacheStoreCentral { if (jsonObject.get("error").getAsBoolean()) { log.error("接口请求错误, 错误信息: {}", jsonObject.get("message").getAsString()); - throw new IOException("Interface Request Error: " + jsonObject.get("message").getAsString()); + throw new HttpRequestException(response.getStatusLine(), responseBody); } long expire = 7200 * 1000; diff --git a/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheHandler.java b/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheHandler.java index a7f88db..ca1b41a 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheHandler.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheHandler.java @@ -1,7 +1,7 @@ package net.lamgc.cgj.bot.cache; import net.lamgc.cgj.bot.boot.BotGlobal; -import net.lamgc.cgj.bot.cache.exception.HttpRequestException; +import net.lamgc.cgj.exception.HttpRequestException; import net.lamgc.cgj.pixiv.PixivURL; import net.lamgc.cgj.util.URLs; import net.lamgc.utils.event.EventHandler; diff --git a/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheStore.java b/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheStore.java index 7763076..5e3fc12 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheStore.java @@ -7,7 +7,7 @@ import com.google.gson.JsonObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import net.lamgc.cgj.bot.cache.exception.HttpRequestException; +import net.lamgc.cgj.exception.HttpRequestException; import java.util.Hashtable; import java.util.Map; diff --git a/src/main/java/net/lamgc/cgj/bot/cache/exception/HttpRequestException.java b/src/main/java/net/lamgc/cgj/exception/HttpRequestException.java similarity index 96% rename from src/main/java/net/lamgc/cgj/bot/cache/exception/HttpRequestException.java rename to src/main/java/net/lamgc/cgj/exception/HttpRequestException.java index f7d6faa..5bfe2c1 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/exception/HttpRequestException.java +++ b/src/main/java/net/lamgc/cgj/exception/HttpRequestException.java @@ -1,4 +1,4 @@ -package net.lamgc.cgj.bot.cache.exception; +package net.lamgc.cgj.exception; import java.io.IOException; import java.util.Objects; diff --git a/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java b/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java index 95b8d16..b04da49 100644 --- a/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java +++ b/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java @@ -6,6 +6,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import io.netty.handler.codec.http.HttpHeaderNames; +import net.lamgc.cgj.exception.HttpRequestException; import org.apache.http.Header; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; @@ -326,7 +327,7 @@ public class PixivDownload { String responseBody = EntityUtils.toString(response.getEntity()); log.trace("ResponseBody: {}", responseBody); if(response.getStatusLine().getStatusCode() != 200) { - throw new IOException("Http Response Error: '" + response.getStatusLine() + "', ResponseBody: '" + responseBody + '\''); + throw new HttpRequestException(response.getStatusLine(), responseBody); } JsonObject resultObject = gson.fromJson(responseBody, JsonObject.class); @@ -353,7 +354,7 @@ public class PixivDownload { HttpResponse response = httpClient.execute(request); if(response.getStatusLine().getStatusCode() != 200) { - throw new IOException("Http响应码非200: " + response.getStatusLine()); + throw new HttpRequestException(response); } Document document = Jsoup.parse(EntityUtils.toString(response.getEntity())); @@ -401,7 +402,7 @@ public class PixivDownload { if(resultObject.get("error").getAsBoolean()) { String message = resultObject.get("message").getAsString(); log.warn("作品页面接口请求错误, 错误信息: {}", message); - throw new IOException(message); + throw new HttpRequestException(response); } JsonArray linkArray = resultObject.getAsJsonArray("body"); @@ -554,7 +555,7 @@ public class PixivDownload { JsonObject responseObj = new Gson().fromJson(responseStr, JsonObject.class); if(responseObj.get("error").getAsBoolean()) { - throw new IOException(responseObj.get("message").getAsString()); + throw new HttpRequestException(response.getStatusLine(), responseStr); } JsonArray illustsArray = responseObj.getAsJsonObject("body").getAsJsonArray("illusts"); diff --git a/src/main/java/net/lamgc/cgj/pixiv/PixivUgoiraBuilder.java b/src/main/java/net/lamgc/cgj/pixiv/PixivUgoiraBuilder.java index 731d3ad..5bc321e 100644 --- a/src/main/java/net/lamgc/cgj/pixiv/PixivUgoiraBuilder.java +++ b/src/main/java/net/lamgc/cgj/pixiv/PixivUgoiraBuilder.java @@ -7,6 +7,7 @@ import com.squareup.gifencoder.GifEncoder; import com.squareup.gifencoder.Image; import com.squareup.gifencoder.ImageOptions; import io.netty.handler.codec.http.HttpHeaderNames; +import net.lamgc.cgj.exception.HttpRequestException; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; @@ -51,7 +52,7 @@ public final class PixivUgoiraBuilder { if(resultObject.get("error").getAsBoolean()) { String message = resultObject.get("message").getAsString(); log.error("获取动图元数据失败!(接口报错: {})", message); - throw new IOException(message); + throw new HttpRequestException(response.getStatusLine(), bodyStr); } else if(!resultObject.has("body")) { String message = "接口返回数据不存在body属性, 可能接口发生改变!"; log.error(message); @@ -181,7 +182,7 @@ public final class PixivUgoiraBuilder { if(resultObject.get("error").getAsBoolean()) { String message = resultObject.get("message").getAsString(); log.error("接口返回错误: {}", message); - throw new IOException(message); + throw new HttpRequestException(response.getStatusLine(), responseBody); } JsonArray illustsArray = resultObject.getAsJsonObject("body").getAsJsonArray("illusts");