Skip to content

Commit 9ef776a

Browse files
[CSAPI]: Log more details on invalid requests
Previously, just the exception message was logged. For some exceptions, this is quite non-descript, for example: 2024-01-25 06:17:14.061 DEBUG [SWAPool-1] - Invalid request (BAD_PAYLOAD): Invalid payload: Invalid XML: Received event END_DOCUMENT, instead of START_ELEMENT or END_ELEMENT. To solve this error, a backtrace is needed to figure out what was expected exactly. This particular exception message could probably be improved by itself, but this commit solves this more generically, since there might be other exceptions suffering from the same issue. This commit changes the handling of InvalidRequestExceptions to use the existing `logError()` function. This logs details of the request and a backtrace. To ensure that this does not log "Internal server error" but keep the existing "Invalid request", `logError()` is refactored to accept an error prefix.
1 parent d111a69 commit 9ef776a

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

sensorhub-service-consys/src/main/java/org/sensorhub/impl/service/consys/RestApiServlet.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public abstract class RestApiServlet extends HttpServlet
5151
{
5252
static final String LOG_REQUEST_MSG = "{} {}{} (from ip={}, user={})";
5353
static final String INTERNAL_ERROR_MSG = "Internal server error";
54-
static final String INTERNAL_ERROR_LOG_MSG = INTERNAL_ERROR_MSG + " while processing request " + LOG_REQUEST_MSG;
54+
static final String ERROR_LOG_MSG = "{} while processing request " + LOG_REQUEST_MSG;
5555
static final String ACCESS_DENIED_ERROR_MSG = "Permission denied";
5656
static final String JSON_CONTENT_TYPE = "application/json";
5757

@@ -384,13 +384,19 @@ protected void logRequest(HttpServletRequest req)
384384

385385

386386
protected void logError(HttpServletRequest req, Throwable e)
387+
{
388+
logError(req, null, e);
389+
}
390+
391+
392+
protected void logError(HttpServletRequest req, String error_prefix, Throwable e)
387393
{
388394
if (log.isErrorEnabled())
389-
logRequestInfo(req, e);
395+
logRequestInfo(req, error_prefix, e);
390396
}
391397

392398

393-
protected void logRequestInfo(HttpServletRequest req, Throwable error)
399+
protected void logRequestInfo(HttpServletRequest req, String error_prefix, Throwable error)
394400
{
395401
String method = req.getMethod();
396402
String url = req.getRequestURI();
@@ -416,7 +422,7 @@ protected void logRequestInfo(HttpServletRequest req, Throwable error)
416422
query = "?" + req.getQueryString();
417423

418424
if (error != null)
419-
log.error(INTERNAL_ERROR_LOG_MSG, method, url, query, ip, user, error);
425+
log.error(ERROR_LOG_MSG, error_prefix, method, url, query, ip, user, error);
420426
else
421427
log.info(LOG_REQUEST_MSG, method, url, query, ip, user);
422428
}
@@ -459,8 +465,8 @@ public void sendError(int code, String msg, HttpServletRequest req, HttpServletR
459465

460466
protected void handleInvalidRequestException(HttpServletRequest req, HttpServletResponse resp, InvalidRequestException e)
461467
{
462-
log.debug("Invalid request ({}): {}", e.getErrorCode(), e.getMessage());
463-
468+
logError(req, String.format("Invalid request (%s)", e.getErrorCode()), e);
469+
464470
switch (e.getErrorCode())
465471
{
466472
case UNSUPPORTED_OPERATION:

0 commit comments

Comments
 (0)