easyWrong PatternsJavaScript
The swallowed error mystery
A developer used AI to generate a file utility function to load application config. Users report that when the config file is missing, the application fails silently and starts up with an empty configuration without any error logs. What is wrong?
config.js
const fs = require('fs');
function loadConfig(filePath) {
try {
const data = fs.readFileSync(filePath, 'utf8');
return JSON.parse(data);
} catch (error) {
// Handle error
return {};
}
}What is the primary bad pattern in this function?
Empty/Swallowed catch block: the error is caught and ignored, returning an empty object which hides failure symptoms.
fs.readFileSync should not be in a try/catch block because it doesn't throw errors.
JSON.parse throws a compile-time error when the file is missing, bypassing the catch block.
The function is missing a finally block to close the file system handle.