forked from Qortal/qortal
Add API error handler to log unexpected errors/exceptions
Fixed /admin/uptime to return text/plain instead of application/json as response is only a simple long. Ditto /admin/stop.
This commit is contained in:
parent
048c54fc0a
commit
315a3c33cf
36
src/main/java/org/qora/api/ApiErrorHandler.java
Normal file
36
src/main/java/org/qora/api/ApiErrorHandler.java
Normal file
@ -0,0 +1,36 @@
|
||||
package org.qora.api;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.server.handler.ErrorHandler;
|
||||
|
||||
public class ApiErrorHandler extends ErrorHandler {
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger(ApiErrorHandler.class);
|
||||
|
||||
@Override
|
||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
String requestURI = request.getRequestURI();
|
||||
|
||||
String queryString = request.getQueryString();
|
||||
if (queryString != null)
|
||||
requestURI += "?" + queryString;
|
||||
|
||||
Throwable th = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
|
||||
if (th != null) {
|
||||
LOGGER.error(String.format("Unexpected %s during request %s", th.getClass().getCanonicalName(), requestURI));
|
||||
} else {
|
||||
LOGGER.error(String.format("Unexpected error during request %s", requestURI));
|
||||
}
|
||||
|
||||
super.handle(target, baseRequest, request, response);
|
||||
}
|
||||
|
||||
}
|
@ -6,6 +6,7 @@ import org.eclipse.jetty.rewrite.handler.RedirectPatternRule;
|
||||
import org.eclipse.jetty.rewrite.handler.RewriteHandler;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.handler.ErrorHandler;
|
||||
import org.eclipse.jetty.server.handler.InetAccessHandler;
|
||||
import org.eclipse.jetty.servlet.DefaultServlet;
|
||||
import org.eclipse.jetty.servlet.FilterHolder;
|
||||
@ -33,6 +34,10 @@ public class ApiService {
|
||||
// Create RPC server
|
||||
this.server = new Server(Settings.getInstance().getApiPort());
|
||||
|
||||
// Error handler
|
||||
ErrorHandler errorHandler = new ApiErrorHandler();
|
||||
this.server.setErrorHandler(errorHandler);
|
||||
|
||||
// IP address based access control
|
||||
InetAccessHandler accessHandler = new InetAccessHandler();
|
||||
for (String pattern : Settings.getInstance().getApiWhitelist()) {
|
||||
@ -49,10 +54,11 @@ public class ApiService {
|
||||
context.setContextPath("/");
|
||||
rewriteHandler.setHandler(context);
|
||||
|
||||
FilterHolder filterHolder = new FilterHolder(CrossOriginFilter.class);
|
||||
filterHolder.setInitParameter("allowedOrigins", "*");
|
||||
filterHolder.setInitParameter("allowedMethods", "GET, POST");
|
||||
context.addFilter(filterHolder, "/*", null);
|
||||
// Cross-origin resource sharing
|
||||
FilterHolder corsFilterHolder = new FilterHolder(CrossOriginFilter.class);
|
||||
corsFilterHolder.setInitParameter("allowedOrigins", "*");
|
||||
corsFilterHolder.setInitParameter("allowedMethods", "GET, POST, DELETE");
|
||||
context.addFilter(corsFilterHolder, "/*", null);
|
||||
|
||||
// API servlet
|
||||
ServletContainer container = new ServletContainer(config);
|
||||
|
@ -59,7 +59,7 @@ public class AdminResource {
|
||||
responses = {
|
||||
@ApiResponse(
|
||||
description = "uptime in milliseconds",
|
||||
content = @Content(schema = @Schema(type = "number"))
|
||||
content = @Content(mediaType = MediaType.TEXT_PLAIN, schema = @Schema(type = "number"))
|
||||
)
|
||||
}
|
||||
)
|
||||
@ -75,7 +75,7 @@ public class AdminResource {
|
||||
responses = {
|
||||
@ApiResponse(
|
||||
description = "\"true\"",
|
||||
content = @Content(schema = @Schema(type = "string"))
|
||||
content = @Content(mediaType = MediaType.TEXT_PLAIN, schema = @Schema(type = "string"))
|
||||
)
|
||||
}
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user