[Fix #11] 修复在图片缓存失效的情况下, 'getImageToBotCode'依然会尝试从缓存获取图片File对象导致NPE;

[Change] BotCommandProcess 调整字符串拼接形式, 统一错误提示语的格式;
This commit is contained in:
2020-06-04 09:29:53 +08:00
parent eb2de09859
commit 69da2b02ac
4 changed files with 125 additions and 57 deletions

View File

@ -1,6 +1,7 @@
package net.lamgc.cgj.bot.cache;
import net.lamgc.cgj.Main;
import net.lamgc.cgj.bot.cache.exception.HttpRequestException;
import net.lamgc.cgj.pixiv.PixivURL;
import net.lamgc.cgj.util.URLs;
import net.lamgc.utils.event.EventHandler;
@ -60,8 +61,9 @@ public class ImageCacheHandler implements EventHandler {
throw e;
}
if(response.getStatusLine().getStatusCode() != 200) {
log.warn("Http请求异常{}", response.getStatusLine());
throw new IOException("Http Response Error: " + response.getStatusLine());
HttpRequestException requestException = new HttpRequestException(response);
log.warn("Http请求异常{}", requestException.getStatusLine());
throw requestException;
}
log.debug("正在下载...(Content-Length: {}KB)", response.getEntity().getContentLength() / 1024);

View File

@ -1,9 +1,14 @@
package net.lamgc.cgj.bot.cache;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.lamgc.cgj.bot.cache.exception.HttpRequestException;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.*;
@ -34,29 +39,30 @@ public final class ImageCacheStore {
* 传递图片缓存任务, 并等待缓存完成.
* @param cacheObject 缓存任务组
*/
public static void executeCacheRequest(ImageCacheObject cacheObject) throws InterruptedException {
public static Throwable executeCacheRequest(ImageCacheObject cacheObject) throws InterruptedException {
Task task = getTaskState(cacheObject);
if(task.taskState.get() == TaskState.COMPLETE) {
return;
return null;
}
boolean locked = false;
try {
if(task.taskState.get() == TaskState.COMPLETE) {
return;
return null;
}
task.lock.lock();
locked = true;
// 双重检查
if(task.taskState.get() == TaskState.COMPLETE) {
return;
return null;
}
// 置任务状态
task.taskState.set(TaskState.RUNNING);
Throwable throwable = null;
try {
Throwable throwable = imageCacheExecutor.submit(() -> {
throwable = imageCacheExecutor.submit(() -> {
try {
handler.getImageToCache(cacheObject);
} catch (Throwable e) {
@ -73,6 +79,7 @@ public final class ImageCacheStore {
} catch (ExecutionException e) {
log.error("执行图片缓存任务时发生异常", e);
}
return throwable;
} finally {
if(locked) {
task.lock.unlock();
@ -88,6 +95,23 @@ public final class ImageCacheStore {
return cacheMap.get(cacheObject);
}
/**
* 获取错误信息
*/
public static String getErrorMessageFromThrowable(Throwable throwable, boolean onlyRequestException) {
if(throwable == null) {
return "";
} else if(!(throwable instanceof HttpRequestException)) {
if(onlyRequestException) {
return "";
}
return throwable.getMessage();
}
JsonObject result = new Gson()
.fromJson(((HttpRequestException) throwable).getContent(), JsonObject.class);
return result.get("msg").getAsString();
}
/**
* 任务状态
*/

View File

@ -0,0 +1,44 @@
package net.lamgc.cgj.bot.cache.exception;
import java.io.IOException;
import java.util.Objects;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.util.EntityUtils;
public class HttpRequestException extends IOException {
private static final long serialVersionUID = -2229221075943552798L;
private final StatusLine statusLine;
private final String content;
public HttpRequestException(HttpResponse response) throws IOException {
this(response.getStatusLine(), EntityUtils.toString(response.getEntity()));
}
public HttpRequestException(StatusLine statusLine, String content) {
super("Http Response Error: " + Objects.requireNonNull(statusLine, "statusLine is null") +
", Response Content: " + (content == null ? "null" : '\'' + content + '\''));
this.statusLine = statusLine;
this.content = content;
}
/**
* 获取Http状态行
*/
public StatusLine getStatusLine() {
return statusLine;
}
/**
* 获取Response内容
* @return 如果没有返回, 则返回null
*/
public String getContent() {
return content;
}
}