Master error handling patterns across languages including exceptions, Result types, error propagation, and graceful degradation to build resilient applications.
Build resilient web applications with robust error handling that gracefully handles failures and provides excellent user feedback — all within Sherif-Auto's vanilla architecture.
Always verify DOM elements exist before manipulation:
const initComponent = () => {
const container = document.querySelector(".component");
if (!container) return; // Silent exit — component not on this page
const button = container.querySelector(".btn");
if (!button) return;
button.addEventListener("click", handleClick);
};
Wrap all network operations:
const submitContactForm = async (formData) => {
const submitBtn = document.querySelector("#submit-btn");
try {
// Show loading state
submitBtn.disabled = true;
submitBtn.classList.add("is-loading");
const db = getFirestore();
await addDoc(collection(db, "contact_requests"), {
...formData,
timestamp: serverTimestamp(),
});
// Success: GSAP animation
showSuccess("Thank you! We'll contact you soon.");
} catch (error) {
// User-friendly error (never expose raw error messages)
showError("Something went wrong. Please try again or call us directly.");
// Log for debugging (remove in production)
// console.error('Form submission error:', error);
} finally {
// Always restore UI state
submitBtn.disabled = false;
submitBtn.classList.remove("is-loading");
}
};
Prevent crashes when ad-blockers or network issues block external scripts:
// Firebase Analytics might be blocked by ad-blockers
window.analytics?.logEvent?.("page_view", { page: "gallery" });
// GSAP might not load from CDN
if (typeof gsap !== "undefined") {
gsap.from(".hero", { opacity: 0, duration: 1 });
} else {
// Fallback: show content immediately without animation
document.querySelector(".hero")?.style.setProperty("opacity", "1");
}
const handleImageError = (img) => {
img.onerror = null; // Prevent infinite loop
img.src = "images/ui/placeholder-vehicle.webp"; // Fallback image
img.alt = "Image unavailable";
};
// Apply to all gallery images
document.querySelectorAll(".gallery-image").forEach((img) => {
img.addEventListener("error", () => handleImageError(img));
});
const renderVehicleCard = (vehicle) => {
// Validate required fields
if (!vehicle || !vehicle.name || !vehicle.brand) {
return ""; // Skip invalid entries silently
}
const thumbnail =
vehicle.thumbnail_path || "images/ui/placeholder-vehicle.webp";
const alt = `${vehicle.brand} ${vehicle.name}`;
return `
<article class="vehicle-card">
<img src="${thumbnail}" alt="${alt}" loading="lazy" width="400" height="300"
onerror="this.src='images/ui/placeholder-vehicle.webp'">
<h3 class="vehicle-card__title">${vehicle.name}</h3>
</article>
`;
};
const showError = (message) => {
const errorEl = document.querySelector(".error-toast");
if (!errorEl) return;
errorEl.textContent = message;
errorEl.classList.add("is-visible");
// Auto-dismiss after 5 seconds
gsap.to(errorEl, {
opacity: 0,
y: -20,
delay: 5,
duration: 0.4,
onComplete: () => errorEl.classList.remove("is-visible"),
});
};
const showSuccess = (message) => {
const successEl = document.querySelector(".success-toast");
if (!successEl) return;
successEl.textContent = message;
gsap.from(successEl, {
opacity: 0,
y: 20,
duration: 0.6,
ease: "power3.out",
});
};
| Category | Example | Strategy |
|---|---|---|
| Expected | Invalid form input | Show specific error message to user |
| Network | Firebase timeout, CDN failure | Retry once, then show friendly fallback |
| Missing DOM | Element not on this page | Silent return (not an error) |
| Data | Missing thumbnail path | Use placeholder, skip entry |
| Third-Party | Analytics blocked by ad-blocker | Optional chaining, graceful skip |
| Critical | Firebase SDK fails to initialize | Show "Contact us by phone" fallback |
finally blocks (buttons, loading spinners)try...catch for every async operation without exceptionconsole.log() before production deploymentDetailed implementation patterns for vanilla JavaScript error handling:
👉 resources/language-patterns.md
Architectural patterns like graceful degradation and error aggregation:
👉 resources/universal-patterns.md
Common mistakes and checklist items:
👉 resources/best-practices.md