[Change] HttpRequestException 统一接口异常, 调整类所在包路径;

This commit is contained in:
LamGC 2020-06-13 16:44:52 +08:00
parent 91e065f657
commit c64320ad78
6 changed files with 13 additions and 10 deletions

View File

@ -8,6 +8,7 @@ import net.lamgc.cgj.bot.BotCode;
import net.lamgc.cgj.bot.BotCommandProcess; import net.lamgc.cgj.bot.BotCommandProcess;
import net.lamgc.cgj.bot.SettingProperties; import net.lamgc.cgj.bot.SettingProperties;
import net.lamgc.cgj.bot.boot.BotGlobal; import net.lamgc.cgj.bot.boot.BotGlobal;
import net.lamgc.cgj.exception.HttpRequestException;
import net.lamgc.cgj.pixiv.PixivDownload; import net.lamgc.cgj.pixiv.PixivDownload;
import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder; import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder;
import net.lamgc.cgj.pixiv.PixivURL; import net.lamgc.cgj.pixiv.PixivURL;
@ -446,7 +447,7 @@ public final class CacheStoreCentral {
if (jsonObject.get("error").getAsBoolean()) { if (jsonObject.get("error").getAsBoolean()) {
log.error("接口请求错误, 错误信息: {}", jsonObject.get("message").getAsString()); 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; long expire = 7200 * 1000;

View File

@ -1,7 +1,7 @@
package net.lamgc.cgj.bot.cache; package net.lamgc.cgj.bot.cache;
import net.lamgc.cgj.bot.boot.BotGlobal; 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.pixiv.PixivURL;
import net.lamgc.cgj.util.URLs; import net.lamgc.cgj.util.URLs;
import net.lamgc.utils.event.EventHandler; import net.lamgc.utils.event.EventHandler;

View File

@ -7,7 +7,7 @@ import com.google.gson.JsonObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.Hashtable;
import java.util.Map; import java.util.Map;

View File

@ -1,4 +1,4 @@
package net.lamgc.cgj.bot.cache.exception; package net.lamgc.cgj.exception;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;

View File

@ -6,6 +6,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaderNames;
import net.lamgc.cgj.exception.HttpRequestException;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
@ -326,7 +327,7 @@ public class PixivDownload {
String responseBody = EntityUtils.toString(response.getEntity()); String responseBody = EntityUtils.toString(response.getEntity());
log.trace("ResponseBody: {}", responseBody); log.trace("ResponseBody: {}", responseBody);
if(response.getStatusLine().getStatusCode() != 200) { 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); JsonObject resultObject = gson.fromJson(responseBody, JsonObject.class);
@ -353,7 +354,7 @@ public class PixivDownload {
HttpResponse response = httpClient.execute(request); HttpResponse response = httpClient.execute(request);
if(response.getStatusLine().getStatusCode() != 200) { if(response.getStatusLine().getStatusCode() != 200) {
throw new IOException("Http响应码非200: " + response.getStatusLine()); throw new HttpRequestException(response);
} }
Document document = Jsoup.parse(EntityUtils.toString(response.getEntity())); Document document = Jsoup.parse(EntityUtils.toString(response.getEntity()));
@ -401,7 +402,7 @@ public class PixivDownload {
if(resultObject.get("error").getAsBoolean()) { if(resultObject.get("error").getAsBoolean()) {
String message = resultObject.get("message").getAsString(); String message = resultObject.get("message").getAsString();
log.warn("作品页面接口请求错误, 错误信息: {}", message); log.warn("作品页面接口请求错误, 错误信息: {}", message);
throw new IOException(message); throw new HttpRequestException(response);
} }
JsonArray linkArray = resultObject.getAsJsonArray("body"); JsonArray linkArray = resultObject.getAsJsonArray("body");
@ -554,7 +555,7 @@ public class PixivDownload {
JsonObject responseObj = new Gson().fromJson(responseStr, JsonObject.class); JsonObject responseObj = new Gson().fromJson(responseStr, JsonObject.class);
if(responseObj.get("error").getAsBoolean()) { 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"); JsonArray illustsArray = responseObj.getAsJsonObject("body").getAsJsonArray("illusts");

View File

@ -7,6 +7,7 @@ import com.squareup.gifencoder.GifEncoder;
import com.squareup.gifencoder.Image; import com.squareup.gifencoder.Image;
import com.squareup.gifencoder.ImageOptions; import com.squareup.gifencoder.ImageOptions;
import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaderNames;
import net.lamgc.cgj.exception.HttpRequestException;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
@ -51,7 +52,7 @@ public final class PixivUgoiraBuilder {
if(resultObject.get("error").getAsBoolean()) { if(resultObject.get("error").getAsBoolean()) {
String message = resultObject.get("message").getAsString(); String message = resultObject.get("message").getAsString();
log.error("获取动图元数据失败!(接口报错: {})", message); log.error("获取动图元数据失败!(接口报错: {})", message);
throw new IOException(message); throw new HttpRequestException(response.getStatusLine(), bodyStr);
} else if(!resultObject.has("body")) { } else if(!resultObject.has("body")) {
String message = "接口返回数据不存在body属性, 可能接口发生改变!"; String message = "接口返回数据不存在body属性, 可能接口发生改变!";
log.error(message); log.error(message);
@ -181,7 +182,7 @@ public final class PixivUgoiraBuilder {
if(resultObject.get("error").getAsBoolean()) { if(resultObject.get("error").getAsBoolean()) {
String message = resultObject.get("message").getAsString(); String message = resultObject.get("message").getAsString();
log.error("接口返回错误: {}", message); log.error("接口返回错误: {}", message);
throw new IOException(message); throw new HttpRequestException(response.getStatusLine(), responseBody);
} }
JsonArray illustsArray = resultObject.getAsJsonObject("body").getAsJsonArray("illusts"); JsonArray illustsArray = resultObject.getAsJsonObject("body").getAsJsonArray("illusts");