<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Michael Interlandi</title>
    <description>The latest articles on DEV Community by Michael Interlandi (@interlandi).</description>
    <link>https://dev.to/interlandi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3899220%2F9b2abaa5-c177-4709-8d37-85ebb3e724f1.jpeg</url>
      <title>DEV Community: Michael Interlandi</title>
      <link>https://dev.to/interlandi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/interlandi"/>
    <language>en</language>
    <item>
      <title>The Utter Failure of the JS Error (and why you should still use JS)</title>
      <dc:creator>Michael Interlandi</dc:creator>
      <pubDate>Mon, 27 Apr 2026 16:20:46 +0000</pubDate>
      <link>https://dev.to/interlandi/the-utter-failure-of-the-js-error-and-why-you-should-still-use-js-3ieh</link>
      <guid>https://dev.to/interlandi/the-utter-failure-of-the-js-error-and-why-you-should-still-use-js-3ieh</guid>
      <description>&lt;h2&gt;
  
  
  The Sin of Simplicity
&lt;/h2&gt;

&lt;p&gt;3.4 million years ago, apes began to demonstrate the ability to use tools.&lt;br&gt;
Impressively, by 1995, those same apes could write dynamic web pages. Back&lt;br&gt;
then, the majority of those apes were writing linear scripts with very few potential failure modes. They had yet to encounter the dense forest of AJAX, or the swamplands of the permissions API and its apex predator, &lt;code&gt;getUserMedia&lt;/code&gt;. No, at this point in history, they were still enchanted by the dry savanna of client-side form validation.&lt;/p&gt;

&lt;p&gt;Back then, the amount of ways a JavaScript application could fail was relatively small, and in the majority of failure cases, it didn't really matter how you responded (if at all). &lt;/p&gt;

&lt;p&gt;Take the following, for example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;validateForm&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 1. Check if the name field is empty&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myForm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please enter your name.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myForm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;focus&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Check if the age is a number&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;ageValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myForm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userAge&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isNaN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ageValue&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please enter a numeric age.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myForm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userAge&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// If all checks pass&lt;/span&gt;
    &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Form is valid! Submitting...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was just about the most complex JS code anyone had written at the time. In this function, the closest thing to failure is the violation of the form invariants.&lt;/p&gt;

&lt;p&gt;Now, let's consider something you might write today.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUserData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/user/1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How many places can this fail? I'll save you the suspense. It's at least this many:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUserData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// failure modes: 400/500 errors; literally any network-related error&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/user/1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

    &lt;span class="c1"&gt;// failure mode: .json() throws if response is empty or invalid JSON&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, how do you handle this? Maybe …&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUserData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/user/1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem? When something does go wrong, we have absolutely no idea what actually failed.&lt;/p&gt;

&lt;p&gt;There's more than one way to handle this, but I'll pick this one for the sake of argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUserData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/user/1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fetch failed:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Failed to parse response as JSON:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, look me in the eyes and tell me you'd actually write the last one at 4:55 PM on a Friday.&lt;/p&gt;

&lt;p&gt;Therein lies the problem. You have to explicitly declare "I would like my consumer to function properly" at every potential failure point. When every single line of code is a potential failure point, &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt;, is just the wrong abstraction. That's opt-in predictability, where predictability can be defined as, at best, the absence of negligence.&lt;/p&gt;

&lt;p&gt;That design philosophy made sense when the happy path was the only path you realistically needed to worry about. Now, though, we write actual libraries and applications with JavaScript. &lt;/p&gt;

&lt;p&gt;As the language grew, its error handling didn't, and now exists purely as a vestige of humble beginnings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simplicity Built on Complexity Is Just Hidden Complexity
&lt;/h2&gt;

&lt;p&gt;Imagine the flow of data through your (JS) program's call stack looks like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fioh216pd39mzachbgz2z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fioh216pd39mzachbgz2z.png" alt="flow chart with boxes reperesenting function calls with arrows representing params and return values between them" width="508" height="854"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Straightforward, right? No.&lt;/p&gt;

&lt;p&gt;The above topology is incomplete. Here's what it actually looks like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3pqn030w88cmzh2pu0t1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3pqn030w88cmzh2pu0t1.png" alt="the same flow chart, but next to it is a similar one where arrows represent errors instead of returned values" width="800" height="621"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, please reread the section title.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Simplicity Built on Complexity Is Just Hidden Complexity&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I'll connect the dots for you in a second, but I'd assume many of you already know where I'm going with this.&lt;/p&gt;

&lt;p&gt;The "hidden complexity" here is the fact that your program can actually fail. I don't know about the rest of the "ape with keyboard" population, but personally, I feel that Brendan Eich may not have had the utmost confidence in our collective intelligence when he made the decision to hide this from us.&lt;/p&gt;

&lt;p&gt;As someone who writes a lot of library-level code, I'm the kind of person this burden ultimately falls on. That said, if you have ever written JavaScript/TypeScript that gets called by anything ever, then you are also that kind of person. &lt;/p&gt;

&lt;p&gt;If you dare to call functions, then you are also affected. Often, you have no way of knowing whether the thing you are using is going to throw unless you read the source. If it's invisible to the type system, it's invisible to the consumer. I need not explain further why this is bad. &lt;/p&gt;

&lt;p&gt;None of this is meant to surprise you or present itself as a novel idea. In fact, if I've done my job right, you've been thinking "yeah, I know" this whole time.&lt;/p&gt;

&lt;p&gt;So, what now?&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Fail Correctly
&lt;/h2&gt;

&lt;p&gt;Rust is not perfect by any means, but I enjoy writing it far more than I enjoy writing TypeScript. A surprising amount of that fact can be attributed to its philosophy around what "errors as values" actually means.&lt;/p&gt;

&lt;p&gt;Since you're already familiar with this form of diagram, here's the Rust version:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqrw8p4iwiin6326z2mz6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqrw8p4iwiin6326z2mz6.png" alt="A similar diagram to above, but with only one column with return, param, and error arrows." width="682" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The keen-eyed among you will have noticed something shocking: the two columns have merged! Instead of insultingly obfuscating your program's error economy from you, Rust forces you to think about it.&lt;/p&gt;

&lt;p&gt;If your eyes were made for the beautiful &lt;code&gt;:&lt;/code&gt; of TypeScript, and not the &lt;code&gt;-&amp;gt;&lt;/code&gt; of Rust, you have permission to close them, but be warned, this code will still be there when you open them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get_user_data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;UserDataError&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/api/user/1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="nf"&gt;.json&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're unfamiliar with the pattern, a &lt;code&gt;Result&amp;lt;T, E&amp;gt;&lt;/code&gt; is just a monoid in the category of endofunctors. You can think of it as a discriminated union that wraps some "good" value &lt;code&gt;T&lt;/code&gt; in the event of a successful call, or some error &lt;code&gt;E&lt;/code&gt; in the event of an error.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;?&lt;/code&gt; operator is syntactic sugar that propagates errors up the call stack. If &lt;code&gt;get()&lt;/code&gt; fails, the function returns early with the error. If &lt;code&gt;response.json()&lt;/code&gt; fails, the function returns early with that error. The caller now has complete visibility into what failed without any &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; boilerplate.&lt;/p&gt;

&lt;p&gt;How do you consume this function? Rust has a number of idioms for dealing with this, some markedly more attractive than others. To showcase one that I'm particularly fond of that isn't the &lt;code&gt;?&lt;/code&gt; operator, I'll show you the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;print_user_data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_user_data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;.unwrap_or_else&lt;/span&gt;&lt;span class="p"&gt;(||&lt;/span&gt; &lt;span class="nf"&gt;get_cached_user_data&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;unwrap_or_else&lt;/code&gt; is very powerful as used above. It allows the expression &lt;code&gt;get_cached_user_data()&lt;/code&gt; to be lazily evaluated, meaning the cache is only invoked when it has to be. &lt;/p&gt;

&lt;p&gt;This is to illustrate that Rust, in being very explicit in its  error handling philosophy, allows you to easily define exactly how much you care about if, how, or in what context something fails, and how you'd like to deal with that failure. &lt;/p&gt;

&lt;p&gt;For example, you have to explicitly tell the compiler that failure is not an issue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// (Usually) won't compile.&lt;/span&gt;
&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;remove_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/tmp/temp_cache"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Failing to remove a temp file is probably fine,&lt;/span&gt;
&lt;span class="c1"&gt;// and this is how we tell the compiler (actually, the linter in this case).&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;remove_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/tmp/temp_cache"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Note: Any binding called _ is not a binding at all, rather,&lt;/span&gt;
&lt;span class="c1"&gt;// a placeholder that tells Clippy, Rust's canonical linter, to ignore a value &lt;/span&gt;
&lt;span class="c1"&gt;// (as is emulated in most of your ESLint configs, I'm sure).&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whereas, in TypeScript you have to explicitly state that failure IS an issue. &lt;/p&gt;

&lt;p&gt;That's a strange thing to have to say about failure.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;neverthrow&lt;/code&gt; and the &lt;code&gt;thiserror&lt;/code&gt;-shaped Hole
&lt;/h2&gt;

&lt;p&gt;If you're really deep in the TypeScript rabbit hole, you know exactly what this section is about. The gorgeous, unapologetically Rust-like time sink that is &lt;a href="https://github.com/supermacro/neverthrow" rel="noopener noreferrer"&gt;&lt;code&gt;neverthrow&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you're unfamiliar, here's everything I knew when I wrote my first line of &lt;code&gt;neverthrow&lt;/code&gt;-flavored TypeScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUserData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchFromAParallelUniverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/user/1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns a neverthrow Result&amp;lt;UserData, SomeErrorType&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isErr&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// This method is on all neverthrow Results&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fetch failed:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// We can extract the inner error using the `error` member.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// We can extract the inner value by using the `value` member.&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isErr&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Failed to parse response as JSON:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// TypeScript knows that value is defined, but error is not at compile time.&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;tryToConsume&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;//                 ^? undefined&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To be clear, &lt;code&gt;neverthrow&lt;/code&gt; isn't the only solution to this problem, but I'd recommend it over something like &lt;a href="https://effect.website/" rel="noopener noreferrer"&gt;Effect&lt;/a&gt; to most people grasping the pattern for the first time, since it doesn't require you to &lt;a href="https://x.com/theo/status/1936198200928747875?s=20" rel="noopener noreferrer"&gt;port your prefrontal cortex to Haskell&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The important thing to note here is that you don't get to use the inner value until you've acknowledged the failure case. This necessarily inverts the usual TypeScript paradigm: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;… in TypeScript you have to explicitly state that failure IS an issue. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, a question is yet unanswered here, and it actually exists in the Rust examples as well:&lt;/p&gt;

&lt;p&gt;What type are the errors?&lt;/p&gt;

&lt;p&gt;The usual TypeScript answer to this question is "I dunno, Error?", and this is actually the case more often than not even with &lt;code&gt;neverthrow&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Within the Rust ecosystem, there are a few good answers. The application developer-sanctioned choice, is &lt;a href="https://docs.rs/anyhow/latest/anyhow/" rel="noopener noreferrer"&gt;&lt;code&gt;anyhow&lt;/code&gt;&lt;/a&gt;. It's effectively a set of tools for working with a single &lt;code&gt;anyhow::Error&lt;/code&gt; type that can consume any error you can think of with minimal keystrokes.&lt;/p&gt;

&lt;p&gt;If you ask a library developer, they'd tell you about &lt;a href="https://docs.rs/thiserror/latest/thiserror/" rel="noopener noreferrer"&gt;&lt;code&gt;thiserror&lt;/code&gt;&lt;/a&gt;, whose claim to fame is its ability to allow you to easily define custom error types, again, with minimal keystrokes. &lt;/p&gt;

&lt;p&gt;In TypeScript, however, this is impossible, because &lt;code&gt;thiserror&lt;/code&gt;'s primary ergonomic benefit is driven entirely by Rust's powerful &lt;a href="https://doc.rust-lang.org/book/ch20-05-macros.html" rel="noopener noreferrer"&gt;metaprogramming&lt;/a&gt; functionality.&lt;/p&gt;

&lt;p&gt;This is the hole. You can get really close, but inverting paradigms is as hard as inverting (I really tried to find a good way to finish this sentence, so use your imagination). Metaprogramming simply does not exist in TypeScript, so if you want to use &lt;code&gt;neverthrow&lt;/code&gt; as divinely ordained, you're gonna be writing some boilerplate.&lt;/p&gt;

&lt;p&gt;And yet …&lt;/p&gt;

&lt;h2&gt;
  
  
  You Should Still Use JS
&lt;/h2&gt;

&lt;p&gt;The point of this article is not to pit JS against Rust. That's a silly comparison. They solve fundamentally different problems for different people with different constraints.&lt;/p&gt;

&lt;p&gt;Rather, my intention was to explore my personal biggest problem with JS, and be specific about what a good solution to that problem actually looks like.  &lt;/p&gt;

&lt;p&gt;Do I think we will ever see Rust-like error handling as a first-class citizen? For a multitude of reasons, almost certainly not (as much as I would like to be proven wrong). &lt;/p&gt;

&lt;p&gt;JS is a microcosm of its own flaw. It's not conceptually attractive, nor philosophically correct, but it ships software.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because the entire planet already depends on this nuclear pile of &lt;code&gt;[object Object]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you have a problem in TypeScript, there's a good solution written in TypeScript. If you have a problem in Rust, there's a solution written in Rust with zero documentation, and a solution written in C with Rust bindings with zero documentation.&lt;/p&gt;

&lt;p&gt;The corpus of public TypeScript code is an order of magnitude larger than that of Rust. This poses more potential benefits than just the above, such as the fact that LLM inference over TypeScript code is categorically better than that over Rust code.&lt;/p&gt;

&lt;p&gt;Hence, if you want to go home at 5:00 o'clock, and don't have a compelling reason to use something else, you should be writing JS/TS.&lt;/p&gt;

&lt;p&gt;Yes, it has absolutely no business being as relied upon as it is.&lt;/p&gt;

&lt;p&gt;Yes, its answer to error handling becomes a burden as soon as you try to step outside the boundaries of what it was originally designed for.&lt;/p&gt;

&lt;p&gt;Yes, it (insert any of the million things wrong with JS).&lt;/p&gt;

&lt;p&gt;Use it anyway.&lt;/p&gt;

&lt;p&gt;The language doesn't deserve you, but here we are.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
