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.')