JuliusBrussee/caveman · error
githubapp: decode token response
Error message
githubapp: decode token response: %w
What it means
The access_tokens endpoint returned 201 but the body could not be decoded into the InstallationToken struct. The underlying json.Unmarshal error is wrapped with this prefix. Without a decodable response the library cannot return a usable token, so it fails.
Solutions
- Inspect the wrapped decode error and raw body to see what came back
- Bypass or fix intermediary proxies that mangle API responses
- Fix mock/fixture payloads to match the real access_tokens response (token, expires_at, permissions, repositories)
- Update the library if GitHub's schema changed
Defensive patterns
Strategy: try-catch
Validate before calling
// No pre-call validation; ensure a clean network path to api.github.com
Type guard
func isDecodeErr(err error) bool {
return err != nil && strings.Contains(err.Error(), "decode token response:")
} Try / catch
tok, err := app.MintInstallationToken(ctx, id, []string{repo}, nil)
if err != nil {
if isDecodeErr(err) {
log.Printf("token response not parseable: %v", err)
// check proxy/mocks; retry once
}
return err
} Prevention
- Avoid TLS-intercepting proxies on api.github.com or pin expectations
- Match mock fixtures to the real access_tokens response schema
- Keep the library current with GitHub API changes
When it happens
Trigger: App.MintInstallationToken gets a 201 whose body is empty, HTML, or otherwise not the expected JSON shape — typically caused by proxies, misconfigured mock servers, or unexpected GitHub responses.
Common situations: Corporate TLS-inspecting proxies altering responses; test fixtures returning wrong-shaped JSON; GitHub API previews with changed payloads.
Understand the failure class
Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.
Related errors
- githubapp: decode installation
- cave response must be a JSON object
- githubapp: get installation: HTTP
- githubapp: mint token: HTTP
- kms: decode response
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/b6fa95d8eef5664e.
Report an issue: GitHub.
Appendix: source
Thrown at shared/platform/githubapp/githubapp.go:221
}
scopedPermissions[name] = level
}
jwt, err := a.AppJWT()
if err != nil {
return InstallationToken{}, err
}
body := map[string]any{"permissions": scopedPermissions, "repositories": []string{repository}}
status, raw, err := a.do(ctx, "Bearer "+jwt, http.MethodPost,
"/app/installations/"+strconv.FormatInt(installationID, 10)+"/access_tokens", body)
if err != nil {
return InstallationToken{}, err
}
if status != http.StatusCreated {
return InstallationToken{}, fmt.Errorf("githubapp: mint token: HTTP %d: %s", status, snippet(raw))
}
var out InstallationToken
if err := json.Unmarshal(raw, &out); err != nil {
return InstallationToken{}, fmt.Errorf("githubapp: decode token response: %w", err)
}
if out.Token == "" {
return InstallationToken{}, fmt.Errorf("githubapp: token response carried no token")
}
return out, nil
}
// RevokeToken DELETEs /installation/token authenticated with the token itself —
// the job-end "drop all agency" step. A best-effort revoke; the ~1h natural
// expiry is the backstop.
func (a *App) RevokeToken(ctx context.Context, token string) error {
status, raw, err := a.do(ctx, "Bearer "+token, http.MethodDelete, "/installation/token", nil)
if err != nil {
return err
}
if status != http.StatusNoContent {
return fmt.Errorf("githubapp: revoke token: HTTP %d: %s", status, snippet(raw))
}View on GitHub (pinned to 3ee70a1026)