hmjz100/LinkSwift · error · Error

[百度网盘] 获取文件 URL 失败

Error message

[百度网盘] 获取文件 URL 失败

What it means

Fallback error thrown in the .catch() of `this.getFilesUrl(files, token)` for the Baidu Netdisk page (home/main). If the underlying rejection is not an Error instance (e.g. a plain string, object, or undefined from a failed fetch/XHR), it is re-wrapped with this generic message; the original message is used when the thrown value has a `message` property.

Source

Thrown at (改)网盘直链下载助手.user.js:6105

			// 获取选择的文件列表
			const selects = this.getSelectedList();
			if (selects.length === 0) throw new Error("提示:<br/>请勾选要下载的文件哦~");

			$doc.find(".loading-popup .loading-title").html(`链接获取中`);
			$doc.find(".loading-popup .swal2-html-container").html(`<div>正在获取文件对应的下载链接~</div>`);

			let files = selects.filter(f => !f.isdir);
			const dirs = selects.filter(f => f.isdir);
			if (temp.page === "home" || temp.page === "main") {
				if (dirs.length > 0) files = files.concat(await this.getFilesList(dirs, token, files.length));
				if (!files.length) throw new Error("提示:<br/>文件夹是空的哦~");

				$doc.find(".loading-popup .loading-title").html(`链接获取中`);
				$doc.find(".loading-popup .swal2-html-container").html(`<div>正在获取文件对应的下载链接~</div>`);

				files = await this.getFilesUrl(files, token).catch(e => {
					if (e instanceof Error) throw e;
					throw new Error(e?.message || e || "[百度网盘] 获取文件 URL 失败");
				});
			} else if (temp.page === "share") {
				const shareData = await this.getShareData();

				const sign = await base.get(`${config.$baidu.api.getShareSign}&surl=1${shareData.share.url}$bdstoken=${shareData.baidu.token}&logid=${base.encodeBase(shareData.baidu.id)}`);
				if (sign?.data?.sign && sign?.data?.timestamp) {
					shareData.sign = sign.data.sign;
					shareData.timestamp = sign.data.timestamp;
				}

				files = await this.getShareFilesUrl(files, shareData, token).catch(e => {
					if (e instanceof Error) throw e;
					throw new Error(e?.message || e || "[百度网盘] 获取分享文件 URL 失败");
				});
			} else {
				throw new Error("提示:<br/>页面错误~");
			}

View on GitHub (pinned to 417ea5e28a)

Solutions

  1. Log out and back into Baidu Netdisk, then re-save the token in the script settings to refresh credentials.
  2. Update the userscript to the latest version to pick up API endpoint fixes.
  3. Retry after a wait — Baidu rate-limits/risk-controls frequent link fetches.
  4. Open DevTools Network tab to inspect the actual failed request and response for the real cause.

Example fix

// before
throw new Error(e?.message || e || "[百度网盘] 获取文件 URL 失败");
// after
const raw = e?.responseText || e?.message || e;
console.error("getFilesUrl failed:", raw); // surface the true upstream cause
Defensive patterns

Strategy: try-catch

Validate before calling

const token = base.getStorage('token');
if (!token || !token.access_token) { alert('请先登录网盘并刷新 token'); return; }

Type guard

function isHttpError(e) { return e instanceof Error || (e && typeof e.message === 'string'); }

Try / catch

try { await getLinks(); } catch (e) { const cause = e && e.cause ? e.cause : e; console.error('Baidu link fetch failed:', cause); if (/401|token/i.test(String(cause))) refreshBaiduToken(); }

Prevention

When it happens

Trigger: getFilesUrl's internal HTTP requests to Baidu's download-link API fail or reject with a non-Error value: expired/invalid access token, API endpoint change, rate limiting, or network failure returning a non-Error rejection.

Common situations: Stale token in script storage after logging out/in; Baidu API change breaking the userscript after a Netdisk frontend update; network blocked or Baidu risk-control rejecting rapid link requests; cookie/BDUSS missing so the dlink API returns an HTML error page.

Understand the failure class

Background: "API request failed": what wrapped HTTP errors from external APIs mean and how to find the real cause — this error's family across 29 libraries.

Related errors


AI-assisted analysis of hmjz100/LinkSwift@417ea5e28a (2026-09-02). Data as JSON: /api/errors/77ba930890b9567a. Report an issue: GitHub.