{"record":{"id":"fabbae057fa7e5f7","repo":"gildas-lormeau/SingleFile","slug":"code-required","errorCode":"code_required","errorMessage":"code_required","messagePattern":"code_required","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/lib/gdrive/gdrive.js","lineNumber":315,"sourceCode":"\t\t\t}\n\t\t\toptions.code = searchParams.get(\"code\");\n\t\t\treturn await authFromCode(gdrive, options);\n\t\t} else if (options.launchWebAuthFlow) {\n\t\t\toptions.extractAuthCode(browser.identity.getRedirectURL(), authFlow)\n\t\t\t\t.then(authCode => code = authCode)\n\t\t\t\t.catch(() => { /* ignored */ });\n\t\t\treturn await options.launchWebAuthFlow({ url: gdrive.authURL }, authFlow);\n\t\t} else {\n\t\t\tthrow new Error(\"auth_not_supported\");\n\t\t}\n\t}\n\tcatch (error) {\n\t\tif (error.message && (error.message == \"code_required\" || error.message.includes(\"access\"))) {\n\t\t\tif (code) {\n\t\t\t\toptions.code = code;\n\t\t\t\treturn await authFromCode(gdrive, options);\n\t\t\t} else {\n\t\t\t\tthrow new Error(\"code_required\", { cause: error });\n\t\t\t}\n\t\t} else {\n\t\t\tthrow error;\n\t\t}\n\t}\n\tfinally {\n\t\tif (authFlow.cancel) {\n\t\t\tauthFlow.cancel();\n\t\t}\n\t}\n}\n\nfunction generateState() {\n\treturn Array.from(crypto.getRandomValues(new Uint8Array(16)))\n\t\t.map(value => value.toString(16).padStart(2, \"0\"))\n\t\t.join(\"\");\n}\n","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/gildas-lormeau/SingleFile/blob/517fb7c5cf2096d89933b747e862d8ecf616a9f9/src/lib/gdrive/gdrive.js#L297-L333","documentation":"initAuth throws \"code_required\" when the auth flow ended without producing an authorization code. The library caught the underlying flow error, found no captured code (the local `code` variable is still unset because extractAuthCode never resolved), and re-throws with the original error as `cause`. It signals that a valid OAuth authorization code must be supplied before auth can complete.","triggerScenarios":"launchWebAuthFlow fails or is closed by the user; extractAuthCode's promise rejects (its .catch swallows the error, leaving `code` undefined); the inner flow throws an error whose message is \"code_required\" or contains \"access\", and options.code was not set.","commonSituations":"User cancels the Google consent popup; the redirect URL is not registered in the Google Cloud OAuth client settings; network failure during the consent flow; passing a stale/expired code while extractAuthCode also fails.","solutions":["Inspect error.cause to learn why the flow failed (redirect URI mismatch, user cancellation, network).","Ensure options.extractAuthCode resolves with the auth code from the redirect URL before the flow promise settles.","Retry the auth flow with interactive: true so the user can complete consent.","As a fallback, obtain an auth code out-of-band and pass options.code so initAuth calls authFromCode directly."],"exampleFix":"// before\nconst token = await gdrive.auth({ launchWebAuthFlow }); // user closed popup -> code_required\n// after\ntry {\n  const token = await gdrive.auth({ launchWebAuthFlow });\n} catch (e) {\n  if (e.message === \"code_required\") {\n    const token = await gdrive.auth({ code: promptForAuthCode(gdrive.authURL) });\n  }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await gdrive.auth(options); }\ncatch (e) {\n  if (e.message === \"code_required\") {\n    const cause = e.cause; // inspect why the flow failed\n    // prompt user again or fall back to manual code entry\n    await gdrive.auth({ ...options, code: await obtainCodeManually() });\n  } else throw e;\n}","preventionTips":["Make the auth flow interactive so users can complete consent","Register the exact redirect URI in the Google OAuth client settings","Always set options.code in options before initiating the flow so partial results can be reused"],"tags":["oauth","auth","user-cancelled","gdrive"],"backgroundTag":"missing-oauth-authorization-code","analyzedSha":"517fb7c5cf2096d89933b747e862d8ecf616a9f9","analyzedAt":"2026-09-01T10:05:25.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}