Hackforge Academy

Category: javascript

How to handle error in frontend

Published on 04 Jun 2026

Explanation

Frontend applications should handle API errors using try-catch blocks to prevent application crashes.

Code:

try { const data = await apiCall(); } 
catch(error) { console.log(error); }

Explanation

Display user-friendly error messages instead of showing technical error details returned by the server.

Code:

setError('Something went wrong. 
Please try again.')

Explanation

Handle HTTP status codes differently based on the response received from the backend.

Code:

if(response.status === 404){ 
showError('Resource not found'); }

Explanation

Show loading, success, and error states to improve user experience during API requests.

Code:

{ loading && 'Loading...' } { 
error && errorMessage }

Explanation

Log errors for debugging while displaying a simple message to users.

Code:

console.error(error); 
showError('
Unable to complete the request.')

๐Ÿš€ Learn Spring Boot with real-world projects

๐Ÿ’ก Build REST APIs step by step

๐Ÿง  Improve backend development skills

๐ŸŽฏ Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

๐Ÿ

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
โš›๏ธ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
โ˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community