{"record":{"id":"7d593094f6d10fbe","repo":"libnyanpasu/clash-nyanpasu","slug":"failed-to-build-download-task-e","errorCode":null,"errorMessage":"failed to build download task: {e}","messagePattern":"failed to build download task: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/tauri/src/core/download/mod.rs","lineNumber":117,"sourceCode":"        url: Url,\n        save_path: PathBuf,\n    ) -> anyhow::Result<Self> {\n        let inner = Arc::new(SessionInner {\n            state: RwLock::new(DownloaderState::Idle),\n            progress: RwLock::new(ProgressSnapshot::default()),\n            cancel: CancellationToken::new(),\n        });\n        let adapter: AnyAdapter = Box::new(NyanpasuReqwestAdapter::new(client, url));\n        let cb_inner = inner.clone();\n        let task = Task::builder()\n            .adapter(adapter)\n            .save_path(save_path)\n            .threaded_runtime(ThreadedRuntimeImpl::new_tokio_rt())\n            .cancel_token(inner.cancel.clone())\n            .on_task_state_changed(move |event| cb_inner.on_event(event))\n            .build()\n            .await\n            .map_err(|e| anyhow::anyhow!(\"failed to build download task: {e}\"))?;\n        Ok(Self {\n            inner,\n            task: Mutex::new(task),\n        })\n    }\n\n    pub async fn start(&self) -> anyhow::Result<()> {\n        let mut task = self.task.lock().await;\n        task.run()\n            .await\n            .map_err(|e| anyhow::anyhow!(\"failed to start download: {e}\"))?;\n        match task.wait().await {\n            Ok(()) => Ok(()),\n            Err(e) => {\n                *self.inner.state.write() = DownloaderState::Failed(e.to_string());\n                Err(anyhow::anyhow!(\"download failed: {e}\"))\n            }\n        }","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/libnyanpasu/clash-nyanpasu/blob/f7dbce2997c633e484f54788035e770b3ee99773/backend/tauri/src/core/download/mod.rs#L99-L135","documentation":"Wraps a failure from the download library's `Task::builder()...build().await` in `DownloadSession::new`. The builder assembles a download task around an adapter (reqwest-based here), a save path and a threaded runtime; if the underlying task cannot be constructed (bad save path, runtime/thread setup failure, adapter error), the error is re-wrapped with this message, so the original cause is embedded in `{e}`.","triggerScenarios":"Constructing `DownloadSession::new(client, url, save_path)` where the download-task builder fails: `save_path` is unwritable/does not exist or cannot be created, the threaded tokio runtime cannot be spawned, or the adapter/task configuration is rejected by the download crate.","commonSituations":"Downloading to a directory that does not exist or lacks write permission (e.g. protected install dir, read-only mount, full disk); invalid URL scheme rejected at task build; environment where spawning the threaded runtime fails (resource limits).","solutions":["Read the wrapped `{e}` cause; ensure `save_path` exists (create it with `std::fs::create_dir_all`) and is writable.","Check disk space and permissions on the target directory.","Validate the URL before constructing the session (scheme, host).","Retry if the failure was a transient runtime-spawn/resource error."],"exampleFix":"// before\nlet session = DownloadSession::new(client, url, save_path.clone()).await?;\n// after\nstd::fs::create_dir_all(&save_path)?;\nlet session = DownloadSession::new(client, url, save_path).await\n    .map_err(|e| { log::error!(\"download task build failed: {e:#}\"); e })?;","handlingStrategy":"try-catch","validationCode":"std::fs::create_dir_all(&save_path)\n    .map_err(|e| anyhow!(\"save path {} unusable: {e}\", save_path.display()))?;\nif url.scheme() != \"http\" && url.scheme() != \"https\" {\n    anyhow::bail!(\"unsupported URL scheme: {}\", url.scheme());\n}","typeGuard":null,"tryCatchPattern":"let session = DownloadSession::new(client, url, save_path).await\n    .with_context(|| format!(\"building download task for {}\", save_path.display()))?;","preventionTips":["Create and permission-check the save directory before constructing the session","Check disk space before large downloads","Validate the URL (scheme, host) before building the task","Log the full error chain ({e:#}) to expose the wrapped builder cause"],"tags":["rust","download","filesystem","builder"],"backgroundTag":"file-write-failed","analyzedSha":"f7dbce2997c633e484f54788035e770b3ee99773","analyzedAt":"2026-09-08T01:24:59.197Z","contentChangedAt":"2026-09-08T01:24:59.197Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}