{"record":{"id":"3301171dbe932888","repo":"git-ecosystem/git-credential-manager","slug":"missing-code-in-response","errorCode":null,"errorMessage":"Missing 'code' in response","messagePattern":"Missing 'code' in response","errorType":"exception","errorClass":"Trace2Exception","httpStatus":null,"severity":"error","filePath":"src/GitHub/GitHubAuthentication.cs","lineNumber":403,"sourceCode":"\n            Context.Console.WriteLine(isSms\n                ? \"An SMS containing the authentication code has been sent to your registered device.\"\n                : \"Use your registered authentication app to generate an authentication code.\");\n\n            return await TerminalPrompts.CreateText(\"Authentication code\").ShowAsync(Context.Console);\n        }\n\n        private async Task<string> GetTwoFactorCodeViaHelperAsync(bool isSms, string args, string command)\n        {\n            var promptArgs = new StringBuilder(args);\n            promptArgs.Append(\"2fa\");\n            if (isSms) promptArgs.Append(\" --sms\");\n\n            IDictionary<string, string> resultDict = await InvokeHelperAsync(command, promptArgs.ToString(), null);\n\n            if (!resultDict.TryGetValue(\"code\", out string authCode))\n            {\n                throw new Trace2Exception(Context.Trace2, \"Missing 'code' in response\");\n            }\n\n            return authCode;\n        }\n\n        public async Task<OAuth2TokenResult> GetOAuthTokenViaBrowserAsync(Uri targetUri, IEnumerable<string> scopes, string loginHint)\n        {\n            ThrowIfUserInteractionDisabled();\n\n            var oauthClient = new GitHubOAuth2Client(HttpClient, Context.Settings, targetUri, Context.Trace2);\n\n            // Can we launch the user's default web browser?\n            if (!Context.SessionManager.IsWebBrowserAvailable)\n            {\n                throw new Trace2InvalidOperationException(Context.Trace2,\n                    \"Browser authentication requires a desktop session\");\n            }\n","sourceCodeStart":385,"sourceCodeEnd":421,"githubUrl":"https://github.com/git-ecosystem/git-credential-manager/blob/e8ce762cd04b4100ae637b5fbf39ef9d0a96561e/src/GitHub/GitHubAuthentication.cs#L385-L421","documentation":"GetTwoFactorCodeViaHelperAsync asks the UI helper for a two-factor authentication code and expects a 'code' key in the response dictionary. If the helper returns without it — the 2FA dialog was abandoned or the helper malfunctioned — this Trace2Exception is thrown because no 2FA code can be returned to the caller.","triggerScenarios":"Invoking GetTwoFactorCodeAsync (e.g. during basic-auth 2FA challenge) and the helper's result dictionary lacks 'code': user closed the 2FA prompt, helper crashed, or an incompatible helper version omitted the key.","commonSituations":"Users skipping the 2FA code entry dialog; helper process failures on machines with broken UI dependencies; SMS 2FA flow interrupted before the code arrives and is entered; legacy helper versions with different output keys.","solutions":["Retry authentication and complete the 2FA dialog by entering the code","Verify GCM/helper versions match and reinstall if the helper is outdated or corrupted","Enable GCM_TRACE and run the helper manually to inspect its raw response for errors","Switch to an auth mode that avoids 2FA prompts (PAT with appropriate scopes, or OAuth browser flow)"],"exampleFix":"// before\nvar code = await auth.GetTwoFactorCodeAsync(uri, isSms: true);\nUseCode(code);\n// after\ntry\n{\n    var code = await auth.GetTwoFactorCodeAsync(uri, isSms: true);\n    UseCode(code);\n}\ncatch (Trace2Exception ex) when (ex.Message == \"Missing 'code' in response\")\n{\n    trace.WriteLine(\"2FA helper returned no code; aborting or retrying.\");\n    throw new TwoFactorCancelledException(ex);\n}","handlingStrategy":"try-catch","validationCode":"// Prefer PAT/OAuth flows that bypass 2FA prompts entirely\n// git config --global credential.github.com.gitHubAuthModes browser,pat\ncode null;","typeGuard":null,"tryCatchPattern":"try\n{\n    string code = await auth.GetTwoFactorCodeAsync(uri, isSms);\n}\ncatch (Trace2Exception ex) when (ex.Message == \"Missing 'code' in response\")\n{\n    // 2FA dialog abandoned or helper failed; surface as cancellation and allow retry\n    throw new TwoFactorCancelledException(ex);\n}","preventionTips":["Complete the 2FA dialog rather than closing it; closing cancels the flow","Use PAT or OAuth browser authentication to avoid interactive 2FA prompts","Keep SMS codes handy before starting the flow so the prompt isn't abandoned","Update GCM/helper as a unit to prevent response-key contract drift"],"tags":["github","two-factor","authentication","helper-process"],"backgroundTag":"response-missing-required-field","analyzedSha":"e8ce762cd04b4100ae637b5fbf39ef9d0a96561e","analyzedAt":"2026-09-11T17:15:08.753Z","contentChangedAt":"2026-09-11T17:15:08.753Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}