{"record":{"id":"f2dc5a4308552097","repo":"sigoden/dufs","slug":"failed-to-fetch-token","errorCode":null,"errorMessage":"Failed to fetch token","messagePattern":"Failed to fetch token","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"assets/index.js","lineNumber":567,"sourceCode":"      try {\n        await checkAuth(\"login\");\n      } catch { }\n      location.reload();\n    });\n  }\n}\n\nfunction setupDownloadWithToken() {\n  document.querySelectorAll(\"a.dlwt\").forEach(link => {\n    link.addEventListener(\"click\", async e => {\n      e.preventDefault();\n      try {\n        const link = e.currentTarget || e.target;\n        const originalHref = link.getAttribute(\"href\");\n        const tokengenUrl = new URL(originalHref);\n        tokengenUrl.searchParams.set(\"tokengen\", \"\");\n        const res = await fetch(tokengenUrl);\n        if (!res.ok) throw new Error(\"Failed to fetch token\");\n        const token = await res.text();\n        const downloadUrl = new URL(originalHref);\n        downloadUrl.searchParams.set(\"token\", token);\n        const tempA = document.createElement(\"a\");\n        tempA.href = downloadUrl.toString();\n        tempA.download = \"\";\n        document.body.appendChild(tempA);\n        tempA.click();\n        document.body.removeChild(tempA);\n      } catch (err) {\n        alert(`Failed to download, ${err.message}`);\n      }\n    });\n  });\n}\n\nfunction setupSearch() {\n  const $searchbar = document.querySelector(\".searchbar\");","sourceCodeStart":549,"sourceCodeEnd":585,"githubUrl":"https://github.com/sigoden/dufs/blob/fe7fd564f80dfbac361c8e0589c3845638149d38/assets/index.js#L549-L585","documentation":"This is a browser-side error thrown by setupDownloadWithToken in dufs's bundled index.js. Before following a download link, the page fetches a one-time token from the link's URL (with tokengen param); if the token request returns a non-2xx response, it throws 'Failed to fetch token' instead of navigating the download, so a stale/invalid token never reaches the actual download URL.","triggerScenarios":"User clicks a download link whose href points at a resource requiring auth, but the tokengen fetch (fetch(tokengenUrl)) responds with res.ok === false — e.g. 401 because the session/token cookies expired, 403 because the user lost write permission, or 404 because the target file was deleted or moved before the click.","commonSituations":"Session expired while the index page stayed open; server restarted and in-memory auth state changed; the file was renamed/removed between page render and click; a proxy stripped the tokengen query param; misconfigured auth rules (--auth) denying the user access to that path.","solutions":["Refresh the page to re-establish session/auth state, then retry the download","Re-login or pass correct credentials so the tokengen endpoint returns 200","Verify the file still exists at the link's href on the server","Check dufs --auth rules allow the current user to access the path","Check server logs / network tab for the tokengen request's actual status code"],"exampleFix":"// before\nconst res = await fetch(tokengenUrl);\nif (!res.ok) throw new Error(\"Failed to fetch token\");\n// after\nconst res = await fetch(tokengenUrl, { credentials: \"same-origin\" });\nif (!res.ok) {\n  if (res.status === 401) { location.href = \"/@login?next=\" + encodeURIComponent(location.href); return; }\n  throw new Error(`Failed to fetch token: HTTP ${res.status}`);\n}","handlingStrategy":"try-catch","validationCode":"const u = new URL(link.getAttribute(\"href\"));\nu.searchParams.set(\"tokengen\", \"\");\nif (!u) throw new Error(\"download link has no href\");","typeGuard":null,"tryCatchPattern":"try {\n  await setupDownloadWithToken(e);\n} catch (err) {\n  if (String(err.message).includes(\"Failed to fetch token\")) {\n    alert(\"Session expired — please refresh the page and log in again.\");\n    location.reload();\n  } else { throw err; }\n}","preventionTips":["Keep sessions alive; re-login before long-lived pages trigger downloads","Use credentials: 'same-origin' on the token fetch","Verify auth rules permit the target path","Check the file still exists before offering the link"],"tags":["javascript","fetch","http-error","auth","browser"],"backgroundTag":"http-error-response","analyzedSha":"fe7fd564f80dfbac361c8e0589c3845638149d38","analyzedAt":"2026-09-09T13:01:22.843Z","contentChangedAt":"2026-09-09T13:01:22.843Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}