Avoid generic catch-all error handling that masks important failures. Instead, be selective about which errors to handle locally versus letting them bubble up to higher-level handlers. When you do handle errors locally, implement safe defaults and use robust parsing methods.
Key principles:
safeJsonParse
instead of raw JSON.parse()
Example of intentional error handling:
// Bad: Generic catch that hides all errors
try {
const result = await response.json();
return result;
} catch (error) {
return { success: false }; // Masks what actually went wrong
}
// Good: Let parsing errors bubble up, handle specific conditions
const result = await safeJsonParse(response);
// Handle specific recoverable errors
if (response.status === 529 || response.status === 503) {
// Retry logic for service overload
return retryRequest();
}
// For security scans, default to safe behavior on failure
try {
const scanResult = await scanRecipe(recipeConfig);
setHasSecurityWarnings(scanResult.has_security_warnings);
} catch (error) {
setHasSecurityWarnings(false); // Safe default, show generic warning
}
Enter the URL of a public GitHub repository