snippetjavascriptTip
Vocal fails are better than silent fails
Viewed 0 times
javascriptbettersilentfailsthanvocalare
Problem
Errors are not your enemy, they're a powerful developer tool. When something goes wrong in your code, an error is your program's way of saying _Hey, something's broken, and here's where to look._ Ignoring or silencing these messages is like turning off a fire alarm because it's too loud. Sure, it's quiet now, but the fire is still raging.
In JavaScript, errors are often thrown when something unexpected happens. Let's look at a very simple example:
This error tells you exactly what went wrong and where. Without it, you'd be left guessing why your program isn't working.
Silent fails occur when errors are caught, but not properly handled or communicated. This can happen in several ways. Let's look at a couple of examples:
In this example, we can see a case of catching a promise and doing nothing. The
In JavaScript, errors are often thrown when something unexpected happens. Let's look at a very simple example:
This error tells you exactly what went wrong and where. Without it, you'd be left guessing why your program isn't working.
Silent fails occur when errors are caught, but not properly handled or communicated. This can happen in several ways. Let's look at a couple of examples:
In this example, we can see a case of catching a promise and doing nothing. The
catch block is empty, meaning if the fetch fails, the error is swallowed, leaving no trace of what went wrong. Down the line, you can end up with unexpected data or behavior, trying to guess what happened.Solution
const data = JSON.parse('invalid JSON'); // Throws a SyntaxErrorThis error tells you exactly what went wrong and where. Without it, you'd be left guessing why your program isn't working.
Silent fails occur when errors are caught, but not properly handled or communicated. This can happen in several ways. Let's look at a couple of examples:
In this example, we can see a case of catching a promise and doing nothing. The
catch block is empty, meaning if the fetch fails, the error is swallowed, leaving no trace of what went wrong. Down the line, you can end up with unexpected data or behavior, trying to guess what happened.Similarly, in this synchronous example, the error is caught, but nothing is done with it, as the
catch block is ignored. The program continues running as if nothing happened, and it may lead to further issues. This approach hides the problem, making debugging nearly impossible.Do you see why this is bad? Silent errors make it hard to pinpoint, debug, and fix issues. This is especially problematic in collaborative projects where team members may not be familiar with every part of the codebase. A silent fail in one module could cascade into unexpected behavior elsewhere, leaving everyone scratching their heads.
Instead of silencing errors, make them vocal and expressive. This doesn't mean you should crash your program at every opportunity, but you should ensure errors are logged, communicated, or handled in a way that provides clarity.
Code Snippets
const data = JSON.parse('invalid JSON'); // Throws a SyntaxErrorfetch('https://api.example.com/data')
.then(response => response.json())
.catch(() => {}); // Silently ignores the errortry {
const result = riskyOperation();
} catch (error) {
// No action taken
}Context
From 30-seconds-of-code: vocal-fails-silencing-errors
Revisions (0)
No revisions yet.