{"record":{"id":"c997046c84d72a73","repo":"siyuan-note/siyuan","slug":"github-api-returned-status-d","errorCode":null,"errorMessage":"GitHub API returned status %d","messagePattern":"GitHub API returned status (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/oidc_provider/provider.go","lineNumber":204,"sourceCode":"\t}\n\treturn user, nil\n}\n\nfunc getGitHubJSON(ctx context.Context, client *http.Client, endpoint string, target any) error {\n\trequest, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)\n\tif err != nil {\n\t\treturn err\n\t}\n\trequest.Header.Set(\"Accept\", \"application/vnd.github+json\")\n\trequest.Header.Set(\"X-GitHub-Api-Version\", \"2022-11-28\")\n\trequest.Header.Set(\"User-Agent\", \"SiYuan\")\n\tresponse, err := client.Do(request)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer response.Body.Close()\n\tif response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {\n\t\treturn fmt.Errorf(\"GitHub API returned status %d\", response.StatusCode)\n\t}\n\tdecoder := json.NewDecoder(io.LimitReader(response.Body, 1024*1024))\n\tdecoder.UseNumber()\n\treturn decoder.Decode(target)\n}\n\nfunc contains(values []string, target string) bool {\n\tfor _, value := range values {\n\t\tif value == target {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n","sourceCodeStart":186,"sourceCodeEnd":219,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/oidc_provider/provider.go#L186-L219","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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)","Verify the OAuth token by calling curl -H 'Authorization: Bearer <token>' https://api.github.com/user","Check X-RateLimit-Remaining/Reset headers and back off if rate limited","Check https://www.githubstatus.com for GitHub API incidents","Inspect proxy/firewall rules if the status code is unexpected (e.g. 407/502 from a proxy)"],"exampleFix":"// before: single status message\nif response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {\n\treturn fmt.Errorf(\"GitHub API returned status %d\", response.StatusCode)\n}\n// after: include status text and rate-limit reset hint\nif response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {\n\treturn fmt.Errorf(\"GitHub API returned status %d (%s), rate limit reset: %s\",\n\t\tresponse.StatusCode, response.Status, response.Header.Get(\"X-RateLimit-Reset\"))\n}","handlingStrategy":"retry","validationCode":"resp, _ := http.Get(\"https://api.github.com/rate_limit\")\n// decode resp: resources.core.remaining > 0 before issuing API calls","typeGuard":null,"tryCatchPattern":"err := getGitHubJSON(ctx, client, url, &target)\nif err != nil {\n\tif strings.Contains(err.Error(), \"status 403\") || strings.Contains(err.Error(), \"status 429\") {\n\t\ttime.Sleep(backoff) // exponential backoff with jitter, then retry\n\t}\n}","preventionTips":["Check X-RateLimit-Remaining before batch API calls","Cache GitHub API responses to reduce call volume","Handle 401 by proactively refreshing tokens","Subscribe to GitHub status updates"],"tags":["go","http","github-api"],"backgroundTag":"http-error-response","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}