<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/rss.xsl" type="text/xsl"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>错误处理 | 折腾啥</title><description>Power Users/Automators 折腾/讨论/分享各种开源工具/脚本/自动化工作流👥 @zhetengsha_group📌 合集 https://t.me/zhetengsha/2🎁 恰饭 https://t.me/zhetengsha/957📢 广告投放 @xream @xream_botBuy ads: https://telega.io/c/zhetengshafeedId:55438372655431680+userId:62307599601855488</description><link>http://telegram.zhetengsha.eu.org</link><item><title>#优质博文 #JavaScript #新特性 #前端 #错误处理Error chaining in JavaScript: cleaner debugging with Error.cause</title><link>http://telegram.zhetengsha.eu.org/posts/4114</link><guid isPermaLink="true">http://telegram.zhetengsha.eu.org/posts/4114</guid><pubDate>Sat, 15 Nov 2025 04:03:40 GMT</pubDate><content:encoded>&lt;div class=&quot;tgme_widget_message_forwarded_from accent_color&quot;&gt;Forwarded from &lt;a class=&quot;tgme_widget_message_forwarded_from_name&quot; href=&quot;https://t.me/cosine_front_end/3053&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;span&gt;cosine - 前端人の日常频道&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;&lt;a href=&quot;/search/result?q=%23%E4%BC%98%E8%B4%A8%E5%8D%9A%E6%96%87&quot; title=&quot;#优质博文&quot;&gt;#优质博文&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23JavaScript&quot; title=&quot;#JavaScript&quot;&gt;#JavaScript&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23%E6%96%B0%E7%89%B9%E6%80%A7&quot; title=&quot;#新特性&quot;&gt;#新特性&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23%E5%89%8D%E7%AB%AF&quot; title=&quot;#前端&quot;&gt;#前端&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86&quot; title=&quot;#错误处理&quot;&gt;#错误处理&lt;/a&gt;&lt;br /&gt;&lt;a href=&quot;https://allthingssmitty.com/2025/11/10/error-chaining-in-javascript-cleaner-debugging-with-error-cause/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot; title=&quot;Error chaining in JavaScript: cleaner debugging with Error.cause&quot;&gt;Error chaining in JavaScript: cleaner debugging with Error.cause&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;tg-expandable&quot;&gt;
      &lt;input type=&quot;checkbox&quot; id=&quot;expand-0-0&quot; class=&quot;tg-expandable__checkbox&quot; aria-label=&quot;Toggle hidden content&quot; aria-controls=&quot;expand-0-0-content&quot; /&gt;
      &lt;div id=&quot;expand-0-0-content&quot; class=&quot;tg-expandable__content&quot;&gt;AI 摘要：本文介绍了在 JavaScript (ES2022) 中引入的 Error.cause 属性，展示它如何让错误处理更具可追溯性。作者 Matt Smith 通过实例说明了传统错误包裹的缺陷，以及 Error.cause 在保持原始堆栈信息、改进调试体验和测试断言方面的优势。最终总结出一套现代错误链最佳实践，帮助开发者构建更清晰、更稳健的错误追踪体系。&lt;/div&gt;
      &lt;label for=&quot;expand-0-0&quot; class=&quot;tg-expandable__toggle&quot;&gt;&lt;span class=&quot;sr-only&quot;&gt;Toggle hidden content&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;br /&gt;[以下是方便搜索索引的大纲(AI 生成)，请读原文]&lt;br /&gt;&lt;div class=&quot;tg-expandable&quot;&gt;
      &lt;input type=&quot;checkbox&quot; id=&quot;expand-0-1&quot; class=&quot;tg-expandable__checkbox&quot; aria-label=&quot;Toggle hidden content&quot; aria-controls=&quot;expand-0-1-content&quot; /&gt;
      &lt;div id=&quot;expand-0-1-content&quot; class=&quot;tg-expandable__content&quot;&gt;1.  传统错误处理的问题&lt;br /&gt;•   层层调用的代码容易丢失原始错误信息；&lt;br /&gt;•   以字符串拼接错误信息会破坏堆栈和错误类型；&lt;br /&gt;•   导致调试困难，追踪难度高。&lt;br /&gt;&lt;br /&gt;2.  引入 Error.cause 的改进&lt;br /&gt;•   通过 new Error(message, { cause }) 保留原始错误；&lt;br /&gt;•   可同时访问顶层与底层堆栈信息；&lt;br /&gt;•   cause 属性为非枚举属性，不污染日志或循环输出；&lt;br /&gt;•   与 message、stack 一样保持一致的行为。&lt;br /&gt;&lt;br /&gt;3.  实践与代码演示&lt;br /&gt;•   示例：从 JSON.parse 到自定义函数的错误传递；&lt;br /&gt;•   在日志中手动输出 err.cause 获取完整上下文；&lt;br /&gt;•   适合层次式系统（如服务嵌套调用）排查问题。&lt;br /&gt;&lt;br /&gt;4.  历史背景与 cause 出现前的替代方案&lt;br /&gt;•   旧方案包括自定义字段 .originalError、字符串拼接等；&lt;br /&gt;•   这些方式不统一且容易破坏原始错误结构；&lt;br /&gt;•   标准化的 cause 提供更安全一致的机制。&lt;br /&gt;&lt;br /&gt;5.  自定义错误类的支持&lt;br /&gt;•   在自定义类中通过 super(message, { cause }) 传递原因；&lt;br /&gt;•   适用 ES2022+ 环境即可直接使用；&lt;br /&gt;•   对 TypeScript 用户，需在 tsconfig.json 中启用 &quot;target&quot;: &quot;es2022&quot; 与 &quot;lib&quot;: [&quot;es2022&quot;]。&lt;br /&gt;&lt;br /&gt;6.  在测试中的应用&lt;br /&gt;•   测试断言可直接验证 err.cause 类型；&lt;br /&gt;•   提升测试可读性与准确性。&lt;br /&gt;&lt;br /&gt;7.  实用技巧与注意事项&lt;br /&gt;•   浏览器默认不输出层级错误链，须手动打印；&lt;br /&gt;•   不建议过度使用错误链，避免干扰阅读；&lt;br /&gt;•   可递归打印完整错误链（logErrorChain 与 logFullErrorChain 示例）；&lt;br /&gt;•   实践场景：数据库异常→业务异常→服务异常的多层错误追踪。&lt;br /&gt;&lt;br /&gt;8.  环境支持与现代用法总结&lt;br /&gt;•   支持于现代浏览器与运行环境（Chrome 93+、Node.js 16.9+、Deno、Bun 等）；&lt;br /&gt;•   推荐实践：&lt;br /&gt;    •   &lt;i class=&quot;emoji&quot;&gt;&lt;b&gt;✅&lt;/b&gt;&lt;/i&gt; 使用 Error(message, { cause })&lt;br /&gt;    •   &lt;i class=&quot;emoji&quot;&gt;&lt;b&gt;✅&lt;/b&gt;&lt;/i&gt; 支持内建与自定义错误类型&lt;br /&gt;    •   &lt;i class=&quot;emoji&quot;&gt;&lt;b&gt;✅&lt;/b&gt;&lt;/i&gt; 改善日志和调试体验&lt;br /&gt;    •   &lt;i class=&quot;emoji&quot;&gt;&lt;b&gt;✅&lt;/b&gt;&lt;/i&gt; 对 TypeScript 进行正确配置&lt;br /&gt;    •   &lt;i class=&quot;emoji&quot;&gt;&lt;b&gt;⚠️&lt;/b&gt;&lt;/i&gt; 记得手动输出或递归追踪错误链。&lt;/div&gt;
      &lt;label for=&quot;expand-0-1&quot; class=&quot;tg-expandable__toggle&quot;&gt;&lt;span class=&quot;sr-only&quot;&gt;Toggle hidden content&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;i&gt;author Matt Smith&lt;/i&gt;&lt;a class=&quot;tgme_widget_message_link_preview&quot; href=&quot;https://allthingssmitty.com/2025/11/10/error-chaining-in-javascript-cleaner-debugging-with-error-cause/?utm_source=substack&amp;amp;utm_medium=email&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot; title=&quot;Use JavaScript&apos;s &apos;cause&apos; property to chain errors, preserve context, and simplify debugging. Cleaner stack traces, better test assertions.&quot;&gt;
  &lt;i class=&quot;link_preview_right_image&quot; style=&quot;background-image:url(&apos;https://cdn4.telesco.pe/file/rsSVYPhVcHXLjUTDyRZzFl-cmwVFLU9cmzqvnl3VUUDVsFfn8T8jIlZL0wV7hqeZFz7RmWyd1I_cPUsRCr78zTyzyvPVgD3suy5rpctNwMCa6ToD4QB8pe1adTh5EAfOzbsO8ThJHOpGGegeRPPb7RpuvVJD6mVggn3eqzIRW4idf8yjCIuRsMiYRd9xH0-yPFEn1dARMTAmtvW-vHSAMuPzhzWV-dkp2LYjB9Yks4-F_78xGdxx_B6EalkoXUrtkC9W2llTcRlQqEn3MKdO_enTzjeQ2NpRQiFZZ8cUs1kZv5GjuKU-EfRPbfcozw3t_ndBS_IawZdAwu8YqqoOKA.jpg&apos;)&quot;&gt;&lt;/i&gt;
  &lt;div class=&quot;link_preview_site_name accent_color&quot;&gt;Allthingssmitty&lt;/div&gt;
  
  &lt;div class=&quot;link_preview_title&quot;&gt;Error chaining in JavaScript: cleaner debugging with Error.cause - Matt Smith&lt;/div&gt;
  &lt;div class=&quot;link_preview_description&quot;&gt;Use JavaScript&apos;s &apos;cause&apos; property to chain errors, preserve context, and simplify debugging. Cleaner stack traces, better test assertions.&lt;/div&gt;
&lt;/a&gt;</content:encoded></item></channel></rss>