Add error logging for OAuth2 code exchange failures in callback#293
Merged
Conversation
Copilot created this pull request from a session on behalf of
veverkap
May 20, 2026 14:45
View session
Contributor
There was a problem hiding this comment.
Pull request overview
Adds structured server-side error logging to the OAuth2 callback when the authorization code exchange fails, improving operational diagnosability while preserving the existing client-facing behavior.
Changes:
- Log
OAuthConfig.Exchange()failures viaslog.ErrorContextimmediately before returning the existing401 failed to exchange coderesponse.
Show a summary per file
| File | Description |
|---|---|
handler/oauth2.go |
Adds structured error logging for OAuth2 code exchange failures in the callback handler. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
OAuth2 callback currently returns
401 failed to exchange codewhenOAuthConfig.Exchange()fails, but does not emit structured error logs. This hides provider/network outage signals and makes operational diagnosis difficult.Problem
handler/oauth2.gocallback path swallowedExchange()failures behind a client-facing 401 without server-side error context.Change
slog.ErrorContextimmediately beforewriteError(...)in theOAuthConfig.Exchange()error branch.401+"failed to exchange code"), adding observability only.Code snippet
Greptile Summary
This PR adds a single
slog.ErrorContextcall inhandler/oauth2.goto emit a structured error log whenOAuthConfig.Exchange()fails during the OAuth2 callback, improving observability without changing the existing401response contract.slog.ErrorContext(r.Context(), \"OAuth2 code exchange failed\", slog.Any(\"error\", err))immediately before thewriteErrorcall in the Exchange failure branch, consistent with the logging pattern used throughout the file for other error paths (e.g.,FetchUserInfo,findOrCreateUser).r.Context()as the first argument, satisfying the project's convention of including acontext.Contextin allslogcalls.Confidence Score: 5/5
This is a safe, additive change that introduces a single structured log line with no effect on request handling or response contracts.
The change is minimal and mechanical: one
slog.ErrorContextcall mirroring the same pattern already used for FetchUserInfo and findOrCreateUser failures elsewhere in the same function. Context is passed correctly, the error key follows project convention, and the HTTP response path is untouched.No files require special attention.
Important Files Changed
slog.ErrorContext(r.Context(), ...)before the existingwriteErrorcall in the Exchange failure branch — follows the established logging pattern throughout the file and correctly passes context.Sequence Diagram
sequenceDiagram participant Browser participant Callback as OAuth2Handler.Callback participant Provider as OAuth2 Provider participant slog as slog (structured log) participant Response as HTTP Response Browser->>Callback: "GET /callback?code=...&state=..." Callback->>Callback: "Validate state cookie & PKCE verifier" Callback->>Provider: OAuthConfig.Exchange(ctx, code, verifier) alt Exchange succeeds Provider-->>Callback: "*oauth2.Token" Callback->>Provider: Provider.FetchUserInfo(ctx, token) Provider-->>Callback: "*OAuth2UserInfo" Callback->>Response: Set JWT/session cookies Callback->>Browser: "302 Redirect /?oauth2_login=1" else Exchange fails (NEW: now logged) Provider-->>Callback: error Callback->>slog: ErrorContext(ctx, "OAuth2 code exchange failed", error) Callback->>Response: 401 "failed to exchange code" Response-->>Browser: 401 JSON error endReviews (1): Last reviewed commit: "Log OAuth2 exchange failures in callback" | Re-trigger Greptile
Context used: