You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何隐藏Node.js中soundcloud-downloader的内置网络错误信息?

解决SoundCloud-Downloader无效URL时错误信息泛滥问题

问题分析

你用catch方法没生效,大概率是因为scdl.getSetInfo内部的错误没有被正确封装为Promise rejection,或是存在未被链式catch捕获的异步错误。改用async/await配合try/catch能更可靠地捕获所有错误,同时可针对性判断错误类型,只输出需要的提示。

修复后的代码

const scdl = require("soundcloud-downloader").default;
const fs = require("fs");

(async() => {
    try {
        const info = await scdl.getSetInfo("https://soundcloud.com/12345/67890");
        console.log(info);
    } catch (reason) {
        // 判断是否是资源不存在的axios错误(404状态)
        if (reason.isAxiosError && reason.response?.status === 404) {
            console.log("Invalid URL!");
        } else {
            // 其他错误也统一输出提示(可按需调整)
            console.log("Invalid URL!");
        }
        // 不打印错误详情,直接忽略
    }
})().catch(err => {
    // 捕获IIFE本身可能出现的未处理错误
    console.log("Invalid URL!");
});

关键说明

  • 用await配合try/catch替代then/catch,能更全面捕获Promise链中的错误,包括库内部可能未正确传递的rejection。
  • 通过reason.isAxiosError和response.status === 404精准判断资源不存在的情况,避免误判其他类型错误。
  • 完全移除错误详情的打印,只输出指定提示内容。

内容的提问来源于stack exchange,提问作者star_huey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 18:59:55