Debugging a Production Issue: Lessons Learned from Solving a Date Parsing Bug in Chromium v110
Published: 2023-03-15 14:18:46

<p>Debugging a production issue can be a daunting task, but it's also an opportunity to learn new things and improve your skills as a developer. Recently, We encountered a production issue where one of the web pages in our application was not working. The issue turned out to be caused by a change in the behaviour of the <code>Date.toLocaleString</code> method, which was used to get AM/PM information. Let's start going through the events, and how it was reported to finally be fixed.</p>
<h3 id="heading-the-problem">The Problem</h3>
<p>One day, we received a support ticket stating that a particular webpage in our application was not working. The page was responsible for inviting new users to the particular account and displaying the details of all the users including when the user was created.</p>
<p>The first step in debugging any production issue is to gather as much information as possible about the problem and try to reproduce the issue locally.</p>
<p>I tried to reproduce the same issue on my machine but it was working fine. I asked my teammate to check if it was working for them. They were able to reproduce the issue. So we checked the console logs and found that there was an error in the Date parsing logic.</p>
<ul>
<li><p>The initial instinct was that this could be an API/Data issue as we haven't done any deployment on the Frontend for some time.</p>
</li>
<li><p>We compared the date format from both browsers where it was working and where it wasn't and it was the same.</p>
</li>
</ul>
<p>Now, the problem seemed weird because it was not happening for all the users. So we decide to check that on different browsers and found that the issue was happening for users on Chromium version &gt;= 110, and it was working for all other browsers and Chromiums version below 110.</p>
<p>In the meantime, we communicated with the reporter that they can use other browsers to get unblocked while we debug the issue.</p>
<p>At this point, we knew something was broken in Chromium v110 but were still not sure what was breaking.</p>
<p>The next step was to run the same piece of code on the browser where it was failing, below is the sample piece of code that was failing in Chromium v110 with the following error</p>
<p><code>Uncaught TypeError: Cannot read properties of undefined (reading 'toLocaleUpperCase')</code></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(createdAt);
<span class="hljs-keyword">const</span> dateArray = date.toLocaleString([], { <span class="hljs-attr">hour12</span>: <span class="hljs-literal">true</span> }).split(<span class="hljs-string">' '</span>);
<span class="hljs-keyword">const</span> time = dateArray[<span class="hljs-number">1</span>];
<span class="hljs-keyword">const</span> ampm = dateArray[<span class="hljs-number">2</span>].toLocaleUpperCase();
</code></pre>
<p>Now, was the time to check the value of <code>dateArray</code> in both browsers, the output was something like this</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(createdAt);
<span class="hljs-keyword">const</span> dateArray = date.toLocaleString([], { <span class="hljs-attr">hour12</span>: <span class="hljs-literal">true</span> }).split(<span class="hljs-string">' '</span>);

<span class="hljs-comment">// ['09/03/2023,', '0:47:09 am'] - Chromium 110</span>
<span class="hljs-comment">// ['09/03/2023,', '0:47:09', 'am'] - Other browsers</span>
</code></pre>
<p>Now we're able to figure out that something is wrong with <code>toLocaleString</code> , to finally confirm the issue, We ran the below piece of code in both browsers.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> dateString = date.toLocaleString([], { <span class="hljs-attr">hour12</span>: <span class="hljs-literal">true</span> });
dateString.charCodeAt(<span class="hljs-number">19</span>);

<span class="hljs-comment">// 8239 - Chromium 110</span>
<span class="hljs-comment">// 32 - Other browsers</span>
</code></pre>
<p>This confirmed that the bug was in the Chromium update and we were able to find the same bug reported in the Chromium project here <a target="_blank" href="https://crbug.com/1414553">https://crbug.com/1414553</a></p>
<h3 id="heading-the-solution">The Solution</h3>
<p>After identifying the root cause of the problem, We began to explore possible solutions. One solution was to update our code to use a different method for parsing the AM/PM information. Since we were using the <code>date-fns</code> library in our project, it was a pretty straightforward way to use the format function from the library and fix the issue</p>
<p>Here's an example of how the updated code looked like:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">return</span> format(createdAt, <span class="hljs-string">'yyyy-MM-dd hh:mm:ss a'</span>) || <span class="hljs-string">''</span>;
  } <span class="hljs-keyword">catch</span> (error) {
    <span class="hljs-keyword">return</span> <span class="hljs-string">''</span>;
  }
</code></pre>
<p>After making these changes, We tested the fix in a staging environment and confirmed that the issue was resolved. We then deployed the fix to the production environment and monitored the logs to ensure that the error did not occur again.</p>
<h3 id="heading-lessons-learned">Lessons Learned</h3>
<ul>
<li><p>Always make sure error handling is done properly in your code, in our case this led to the blank page</p>
</li>
<li><p>Never rely on a browser <code>toLocaleString</code> to parse date information, rather than use the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts">formatToParts</a> method which is reliable for parsing such information or you can use a dedicated library like <code>date-fns</code></p>
</li>
</ul>
<h3 id="heading-conclusion">Conclusion</h3>
<p>In conclusion, debugging production issues can be challenging, but it's important to approach the problem systematically and gather as much information as possible before attempting to fix it. In this case, the issue was caused by a change in the behaviour of the <code>Date.toLocaleString</code> method, which required a code update to handle the new behaviour. By understanding the root cause of the issue and implementing a proper fix, We were able to restore the functionality of the affected web page and prevent the issue from happening again in the future.</p>
</br><p style="color:#757CF9;">This article is published w/ <a target="_blank" href="https://scattr.io?ref=dev">Scattr  ↗️</a></p>