EasyPro challengePythonJavaScriptTypeScript
Fail Fast — Don't Ignore Errors
Clean CodeError HandlingFail Fast
Silently swallowing errors hides bugs and makes debugging a nightmare.
// dirty — errors are swallowed
function solve(json) {
try {
return JSON.parse(json);
} catch (e) {
return null; // swallowed — caller has no idea what went wrong
}
}Refactor solve(json) so failure is never silent: on success return { ok: true, value: <parsed> }, on failure return { ok: false, error: 'Invalid JSON' } — never a bare null that hides which case happened.
Sample tests
Test #1Valid JSON is parsed
Input: ["{\"a\":1}"]
Output: {"ok":true,"value":{"a":1}}
Test #2Valid array JSON
Input: ["[1,2,3]"]
Output: {"ok":true,"value":[1,2,3]}
Test #3Invalid JSON reports failure explicitly
Input: ["invalid"]
Output: {"ok":false,"error":"Invalid JSON"}