SpinlockLabs / github.dart

GitHub Client Library for Dart
https://pub.dev/packages/github
MIT License
408 stars 142 forks source link

When GitHub reports an error, the API just throws a TypeError instead of throwing a useful exception #411

Open Hixie opened 1 day ago

Hixie commented 1 day ago

For example, I tried calling addLabelsToIssue on an issue that, as it turns out, had been deleted. Instead of a useful exception telling me the response from GitHub, I got type '_Map<String, dynamic>' is not a subtype of type 'List<dynamic>' (_TypeError) at GitHub.requestJson (package:github/src/common/github.dart:335:35), which it turns out is not at all useful. (I had to add prints in the GitHub package to figure out what the problem was.)

For what it's worth, the response from GitHub looked like this:

{"message":"Validation Failed","errors":[{"resource":"Label","code":"unprocessable","field":"data","message":"Could not resolve to a node with the global id of 'I_kwDOAeUeuM6artTz'."}],"documentation_url":"https://docs.github.com/rest/issues/labels#add-labels-to-an-issue","status":"422"}

It seems like it's probably the case that we could add some general code to catch errors for all API calls and throw some general exceptions that encode the kinds of information GitHub returns. (Maybe HTTP status codes other than 200 should always throw, or something.)

github-actions[bot] commented 1 day ago

👋 Thanks for reporting! @robrbecker will take a look.