Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detailed logging of invalid requests #247

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public abstract class RestApiServlet extends HttpServlet
{
static final String LOG_REQUEST_MSG = "{} {}{} (from ip={}, user={})";
static final String INTERNAL_ERROR_MSG = "Internal server error";
static final String INTERNAL_ERROR_LOG_MSG = INTERNAL_ERROR_MSG + " while processing request " + LOG_REQUEST_MSG;
static final String ERROR_LOG_MSG = "{} while processing request " + LOG_REQUEST_MSG;
static final String ACCESS_DENIED_ERROR_MSG = "Permission denied";
static final String JSON_CONTENT_TYPE = "application/json";

Expand Down Expand Up @@ -379,18 +379,24 @@ public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse
protected void logRequest(HttpServletRequest req)
{
if (log.isInfoEnabled())
logRequestInfo(req, null);
logRequestInfo(req, null, null);
}


protected void logError(HttpServletRequest req, Throwable e)
{
logError(req, null, e);
}


protected void logError(HttpServletRequest req, String error_prefix, Throwable e)
{
if (log.isErrorEnabled())
logRequestInfo(req, e);
logRequestInfo(req, error_prefix, e);
}


protected void logRequestInfo(HttpServletRequest req, Throwable error)
protected void logRequestInfo(HttpServletRequest req, String error_prefix, Throwable error)
{
String method = req.getMethod();
String url = req.getRequestURI();
Expand All @@ -416,7 +422,7 @@ protected void logRequestInfo(HttpServletRequest req, Throwable error)
query = "?" + req.getQueryString();

if (error != null)
log.error(INTERNAL_ERROR_LOG_MSG, method, url, query, ip, user, error);
log.error(ERROR_LOG_MSG, error_prefix, method, url, query, ip, user, error);
else
log.info(LOG_REQUEST_MSG, method, url, query, ip, user);
}
Expand Down Expand Up @@ -459,8 +465,8 @@ public void sendError(int code, String msg, HttpServletRequest req, HttpServletR

protected void handleInvalidRequestException(HttpServletRequest req, HttpServletResponse resp, InvalidRequestException e)
{
log.debug("Invalid request ({}): {}", e.getErrorCode(), e.getMessage());
logError(req, String.format("Invalid request (%s)", e.getErrorCode()), e);

switch (e.getErrorCode())
{
case UNSUPPORTED_OPERATION:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ public static InvalidRequestException badRequest(String msg)

public static InvalidRequestException invalidPayload(String msg)
{
return new InvalidRequestException(ErrorCode.BAD_PAYLOAD, msg);
return invalidPayload(msg, null);
}


public static InvalidRequestException invalidPayload(String msg, Throwable cause)
{
return new InvalidRequestException(ErrorCode.BAD_PAYLOAD, msg, cause);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ protected void create(final RequestContext ctx) throws IOException
}
catch (ResourceParseException e)
{
throw ServiceErrors.invalidPayload("Invalid payload: " + e.getMessage());
throw ServiceErrors.invalidPayload("Invalid payload: " + e.getMessage(), e);
}
}

Expand Down Expand Up @@ -399,7 +399,7 @@ protected void update(final RequestContext ctx, final String id) throws IOExcept
}
catch (ResourceParseException e)
{
throw ServiceErrors.invalidPayload("Invalid payload: " + e.getMessage());
throw ServiceErrors.invalidPayload("Invalid payload: " + e.getMessage(), e);
}

// update in datastore
Expand Down
Loading