github-api icon indicating copy to clipboard operation
github-api copied to clipboard

GHRepository.getFileContent should give helpful error message when misused

Open LorenzNickel opened this issue 5 years ago • 2 comments

Description

When you misuse GHRepository.getFileContent a com.fasterxml.jackson.databind.exc.MismatchedInputException is thrown.

Reproduce

Create an authorized instance Github github (for example using new GitHubBuilder().withOAuthToken(token).build();)

Execute this: gitHub.getRepository("github-api/github-api").getFileContent("");

You should get something like this: org.kohsuke.github.HttpException: Server returned HTTP response code: 200, message: '200 OK' for URL: https://api.github.com/repos/github-api/github-api/contents/ ... Caused by: java.io.IOException: Failed to deserialize ... Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of 'org.kohsuke.github.GHContent' out of START_ARRAY token at ...

Fix

Please throw a a proper exception or redesign your implementation to avoid such errors.


I'd be really happy if you could fix this, please keep up your great work!

LorenzNickel avatar Apr 03 '20 20:04 LorenzNickel

Hm, yeah, data binding errors are pretty rare for this project. When they do occur they almost always indicate some kind of misuse/mistake on the user's part. So, it totally makes sense to wrap those in another exception with a helpful message.

In this case that message would be something like: "Wanted a GHContent object but received an array. Did you mean to use getDirectoryContent()?"

bitwiseman avatar Apr 03 '20 21:04 bitwiseman

That would for sure help. I still don't know how and why this happens and I haven't dived into the code, but the best thing would probably be to not have this method by design for objects on which this method can't be called. But as I said, I have neither looked at the code of the repo nor a good understanding of GitHub's API, so probably it's up to you if and how you can improve this.

LorenzNickel avatar Apr 03 '20 21:04 LorenzNickel