mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-24 16:14:26 +08:00
Add Exception Handling when an error at the API happens
This commit is contained in:
parent
c37b0c8941
commit
dcff04b73a
@ -34,6 +34,7 @@ import java.util.*;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
/**
|
||||
@ -300,23 +301,21 @@ public class WebServer {
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
return api.onResponse(plan, variables);
|
||||
try {
|
||||
return api.onResponse(plan, variables);
|
||||
} catch (Exception ex) {
|
||||
Log.toLog("WebServer.getAPIResponse", ex);
|
||||
return new InternalErrorResponse(ex, "An error while processing the request happened");
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> readVariables(String response) {
|
||||
Map<String, String> variableMap = new HashMap<>();
|
||||
String[] variables = response.split("&");
|
||||
|
||||
for (String variable : variables) {
|
||||
String[] splittedVariables = variable.split("=", 2);
|
||||
if (splittedVariables.length != 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
variableMap.put(splittedVariables[0], splittedVariables[1]);
|
||||
}
|
||||
|
||||
return variableMap;
|
||||
return Arrays.stream(variables)
|
||||
.map(variable -> variable.split("=", 2))
|
||||
.filter(splittedVariables -> splittedVariables.length == 2)
|
||||
.collect(Collectors.toMap(splittedVariables -> splittedVariables[0], splittedVariables -> splittedVariables[1], (a, b) -> b));
|
||||
}
|
||||
|
||||
private Response getResponse(String target, WebUser user) {
|
||||
|
@ -9,18 +9,23 @@ import main.java.com.djrapitops.plan.ui.html.Html;
|
||||
public class InternalErrorResponse extends Response {
|
||||
|
||||
public InternalErrorResponse(Throwable e, String cause) {
|
||||
super.setHeader("HTTP/1.1 500 Internal Error");
|
||||
StringBuilder content = new StringBuilder();
|
||||
|
||||
super.setHeader("HTTP/1.1 500 Internal Error");
|
||||
|
||||
content.append("<h1>500 Internal Error occurred</h1>");
|
||||
content.append("<p>Please report this issue here: </p>");
|
||||
content.append(Html.LINK.parse("https://github.com/Rsl1122/Plan-PlayerAnalytics/issues", "Issues"));
|
||||
content.append("<p>");
|
||||
content.append(e).append(" | ").append(cause);
|
||||
for (Object element : e.getStackTrace()) {
|
||||
|
||||
for (StackTraceElement element : e.getStackTrace()) {
|
||||
content.append("<br>");
|
||||
content.append(" ").append(element);
|
||||
}
|
||||
|
||||
content.append("</p>");
|
||||
|
||||
super.setContent(content.toString());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user