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

Rework exception handling to attach original exception. #125

merged 3 commits into from
Jan 12, 2018

Conversation

tiaanl
Copy link
Contributor

@tiaanl tiaanl commented Jan 9, 2018

When an errors occurs in the JSON parsing of the WebHookManager then the exceptions is caught, but completely new exceptions are throws, throwing away any useful information about the original cause. By creating the GitlabApiException with the original exception allows you to use ex.getCause() to get to the original data without changing which exceptions are throws from the method.

} 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.

@gmessner gmessner merged commit 320e88c into gitlab4j:master Jan 12, 2018
@gmessner
Copy link
Collaborator

@tiaanl
Thanks for the contribution and working with me on this.

@gmessner
Copy link
Collaborator

@tiaanl
This has been released in the latest release of GitLab4J-API (4.7.15).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants