siyuan-note/siyuan · error

GitHub API returned status

Error message

GitHub API returned status %d

What it means

getGitHubJSON checks the HTTP response status of calls to the GitHub REST API and returns "GitHub API returned status %d" for any status outside 200-299. It is the generic guard ensuring the caller never decodes an error body as the expected JSON payload.

Solutions

  1. Read the status code: 401 -> re-authenticate and get a new token; 403 -> check rate-limit headers; 404/403-scope -> add the required OAuth scopes (user, user:email)
  2. Verify the OAuth token by calling curl -H 'Authorization: Bearer <token>' https://api.github.com/user
  3. Check X-RateLimit-Remaining/Reset headers and back off if rate limited
  4. Check https://www.githubstatus.com for GitHub API incidents
  5. Inspect proxy/firewall rules if the status code is unexpected (e.g. 407/502 from a proxy)

Example fix

// before: single status message
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
	return fmt.Errorf("GitHub API returned status %d", response.StatusCode)
}
// after: include status text and rate-limit reset hint
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
	return fmt.Errorf("GitHub API returned status %d (%s), rate limit reset: %s",
		response.StatusCode, response.Status, response.Header.Get("X-RateLimit-Reset"))
}
Defensive patterns

Strategy: retry

Validate before calling

resp, _ := http.Get("https://api.github.com/rate_limit")
// decode resp: resources.core.remaining > 0 before issuing API calls

Try / catch

err := getGitHubJSON(ctx, client, url, &target)
if err != nil {
	if strings.Contains(err.Error(), "status 403") || strings.Contains(err.Error(), "status 429") {
		time.Sleep(backoff) // exponential backoff with jitter, then retry
	}
}

Prevention

When it happens

Trigger: Any getGitHubJSON call (from exchangeGitHubClaims) receiving e.g. 401 (bad/expired OAuth token), 403 (rate limit or forbidden), 404 (missing endpoint/scope), or 5xx from api.github.com.

Common situations: Access token revoked mid-session; OAuth app configured without required scopes; hitting GitHub's 60 req/hour unauthenticated-equivalent limits on shared IPs; GitHub incidents; corporate proxy injecting 407/502 responses.

Understand the failure class

Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/c997046c84d72a73. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/oidc_provider/provider.go:204

	}
	return user, nil
}

func getGitHubJSON(ctx context.Context, client *http.Client, endpoint string, target any) error {
	request, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
	if err != nil {
		return err
	}
	request.Header.Set("Accept", "application/vnd.github+json")
	request.Header.Set("X-GitHub-Api-Version", "2022-11-28")
	request.Header.Set("User-Agent", "SiYuan")
	response, err := client.Do(request)
	if err != nil {
		return err
	}
	defer response.Body.Close()
	if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
		return fmt.Errorf("GitHub API returned status %d", response.StatusCode)
	}
	decoder := json.NewDecoder(io.LimitReader(response.Body, 1024*1024))
	decoder.UseNumber()
	return decoder.Decode(target)
}

func contains(values []string, target string) bool {
	for _, value := range values {
		if value == target {
			return true
		}
	}
	return false
}

View on GitHub (pinned to 9f775e8a12)