If one runs a drone-cli command and gets just the following, it’s very confusing:
invalid character '<' looking for beginning of value
The most likely explanation for the error above is that
DRONE_SERVER points to an HTTP server that’s not Drone. Perhaps the hostname is wrong. Perhaps the DNS isn’t resolving like one would expect. Perhaps Drone is in an internal network and we’re outside said network.
In our case, we set up an oauth2 proxy and put Drone behind it, so hitting up the API without a valid oauth2 token started giving you a 200 “oauth2 log in with Google” page. Which is HTML, so the first non-whitespace character is a ‘<’ - hence the confusing error when trying to parse that as JSON.
This is a common source of confusion:
Thus, I propose that we handle this specific kind of JSON syntax error via json package - encoding/json - pkg.go.dev, and add a bit of context to the error. For example:
invalid character '<' looking for beginning of value; seems like we got an HTML page on an API call, perhaps not a Drone server?
Whatever extra context pointing to the very likely cause will save quite a few users lots of time. Happy to submit the simple patch if this makes sense.