-
Notifications
You must be signed in to change notification settings - Fork 474
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
Conversation
…itlabApiException.
} 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()); |
There was a problem hiding this comment.
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());
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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() ?
There was a problem hiding this comment.
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);
}
There was a problem hiding this comment.
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.
@tiaanl |
@tiaanl |
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 useex.getCause()
to get to the original data without changing which exceptions are throws from the method.