Skip to content

Rework exception handling to attach original exception. #125

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

Merged
merged 3 commits into from
Jan 12, 2018
Merged
Changes from 1 commit
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
20 changes: 5 additions & 15 deletions src/main/java/org/gitlab4j/api/webhook/WebHookManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public void handleEvent(HttpServletRequest request) throws GitLabApiException {
throw new GitLabApiException(message);
}

String errorMessage = null;
try {

Event event;
Expand All @@ -98,22 +97,13 @@ public void handleEvent(HttpServletRequest request) throws GitLabApiException {

fireEvent(event);

} catch (JsonParseException jpe) {
errorMessage = jpe.getMessage();
LOG.warning("Error parsing JSON data, error=" + errorMessage);
} catch (JsonMappingException jme) {
errorMessage = jme.getMessage();
LOG.warning("Error mapping JSON data, error=" + errorMessage);
} catch (IOException ioe) {
errorMessage = ioe.getMessage();
LOG.warning("Error reading JSON data, error=" + errorMessage);
} catch (IOException e) {
LOG.warning("Error parsing JSON data, error=" + e.getMessage());
throw new GitLabApiException(e);
} catch (Exception e) {
errorMessage = e.getMessage();
LOG.warning("Unexpected error reading JSON data, error=" + errorMessage);
LOG.warning("Unexpected error reading JSON data, error=" + e.getMessage());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you have already simplified it by removing other catch blocks, can you make the logged message more generic and only capture Exception and remove the catch (IOException e) block? Something like this:

LOG.warning("Unexpected error processing JSON data, error=" + e.getMessage());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still a distinction between IOException which is the base of both JsonParseException and JsonMappingException and just a plain Exception. So going through logs and seeing the first message tells me there was likely a parsing error, but seeing the latter message means something really unexpected happened, which i find useful.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tiaanl
SInce your change sets the cause, can you not get that info from the exception returned by using getCause() ?

Copy link
Collaborator

@gmessner gmessner Jan 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tiaanl
How about the following? Simplifies, but still gives the exact reason for the exception in the log:

} catch (Exception e) {
    LOG.warning("Error processing JSON data, exception=" + e.getClass().getSimpleName() + ", error=" + e.getMessage());
    throw new GitLabApiException(e);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that'll work perfectly. Can see info in logs and expect error programmatically. Added commit.

throw new GitLabApiException(e);
}

if (errorMessage != null)
throw new GitLabApiException(errorMessage);
}

/**
Expand Down