jdx / mise

dev tools, env vars, task runner
https://mise.jdx.dev
MIT License
8.07k stars 211 forks source link

Github API rate limiting could be handled more explicitly #2263

Closed thnee closed 3 weeks ago

thnee commented 3 weeks ago

Description

Currently, when being rate limited by the Github API, mise displays a fairly generic error message. As a user, I have to manually debug it to figure out what happened.

It would be good if mise would show a more detailed error message. Either a custom error message like API request has been rate limited by Github, or display the information from the API response. That would make it easier and faster to understand why the command failed.

The documentation says:

If you exceed your primary rate limit, you will receive a 403 or 429 response, and the x-ratelimit-remaining header will be 0.

https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#exceeding-the-rate-limit

So I believe that should make it clear how to implement this, in order to handle this error.

Details

When doing self-update, I get the following result:

λ export RUST_BACKTRACE=full

λ mise version --verbose
[DEBUG] ARGS: /home/username/.local/bin/mise version --verbose
2024.5.14 linux-x64 (c2858ef 2024-05-14)
[WARN] mise version 2024.6.1 available
[WARN] To update, run mise self-update

λ mise self-update --verbose
[DEBUG] ARGS: /home/username/.local/bin/mise self-update --verbose
[DEBUG] starting new connection: https://api.github.com/
Error:
   0: NetworkError: api request failed with status: 403 - for: "https://api.github.com/repos/jdx/mise/releases"

Location:
   src/cli/self_update.rs:59

Version:
   2024.5.14 linux-x64 (c2858ef 2024-05-14)

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
                                ⋮ 2 frames hidden ⋮
   3: mise::cli::self_update::SelfUpdate::run::h457406eb12fd1b04
      at <unknown source file>:<unknown line>
   4: mise::cli::Commands::run::h949a1c0de65cf2ae
      at <unknown source file>:<unknown line>
   5: mise::cli::Cli::run::h2275f2ccbe59a947
      at <unknown source file>:<unknown line>
   6: mise::main::ha27c9dc0b3d34305
      at <unknown source file>:<unknown line>
   7: std::sys_common::backtrace::__rust_begin_short_backtrace::h128a9093386c5f2a
      at <unknown source file>:<unknown line>
   8: main<unknown>
      at <unknown source file>:<unknown line>
   9: __libc_start_call_main<unknown>
      at ./csu/../sysdeps/nptl/libc_start_call_main.h:58
  10: __libc_start_main_impl<unknown>
      at ./csu/../csu/libc-start.c:392
  11: _start<unknown>
      at <unknown source file>:<unknown line>

Run with COLORBT_SHOW_HIDDEN=1 environment variable to disable frame filtering.

Apparently, this is because the API request has been rate limited.

When calling this URL manually, it gives this response:

Headers:

HTTP/2 403 
date: Fri, 07 Jun 2024 09:29:04 GMT
server: Varnish
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-content-type-options: nosniff
x-frame-options: deny
x-xss-protection: 1; mode=block
content-security-policy: default-src 'none'; style-src 'unsafe-inline'
access-control-allow-origin: *
access-control-expose-headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-RateLimit-Used, X-RateLimit-Resource, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset
content-type: application/json; charset=utf-8
referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin
x-github-media-type: github.v3; format=json
x-ratelimit-limit: 60
x-ratelimit-remaining: 0
x-ratelimit-reset: 1717755536
x-ratelimit-resource: core
x-ratelimit-used: 60
content-length: 278
x-github-request-id: B84B:2B4C97:1385B665:13A3C54B:6662D2DF
X-Firefox-Spdy: h2

Body:

{"message":"API rate limit exceeded for z.x.c.v. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)","documentation_url":"https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting"}

(Where z.x.c.v is my numeric IP address).

jdx commented 3 weeks ago

mise supports GITHUB_API_KEY however I don't think this is mentioned in the docs, I would love it if someone would add a PR for that