0
0
Angularframework~10 mins

Handling HTTP errors in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Handling HTTP errors
Start HTTP Request
Receive HTTP Response
Is response error?
NoProcess Data
Yes
Catch Error
Handle Error (show message/log)
Complete or Retry
This flow shows how Angular sends an HTTP request, checks if the response has an error, and handles it gracefully.
Execution Sample
Angular
this.http.get('/api/data').pipe(
  catchError(error => {
    this.errorMessage = 'Failed to load data';
    return throwError(() => error);
  })
).subscribe(data => this.data = data);
This code sends a GET request, catches errors to set a message, and updates data on success.
Execution Table
StepActionHTTP ResponseError DetectedError HandlingData StateMessage State
1Send GET requestPendingNoNoundefined''
2Receive response200 OK with dataNoNoundefined''
3Subscribe success200 OK with dataNoNodata loaded''
4Send GET requestPendingNoNodata loaded''
5Receive response500 Internal Server ErrorYesYesdata loadedFailed to load data
6Subscribe error500 Internal Server ErrorYesYesdata loadedFailed to load data
💡 Execution stops after handling error or successfully loading data.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6
dataundefinedundefineddata loadeddata loadeddata loaded
errorMessage''''''Failed to load dataFailed to load data
Key Moments - 3 Insights
Why does the errorMessage only update after an error response?
Because catchError runs only when an error occurs, as shown in steps 5 and 6 of the execution_table.
What happens to the data variable when an error occurs?
The data variable keeps its previous value and does not update, as seen in steps 5 and 6.
Why do we return throwError inside catchError?
Returning throwError rethrows the error so subscribers can handle it further if needed, matching the flow in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of errorMessage at step 3?
A'Loading...'
B'Failed to load data'
C'' (empty string)
Dundefined
💡 Hint
Check the 'Message State' column at step 3 in the execution_table.
At which step does the HTTP error get detected?
AStep 5
BStep 4
CStep 2
DStep 3
💡 Hint
Look for 'Error Detected' marked 'Yes' in the execution_table.
If the server returns 404 instead of 500, how would the errorMessage change in the variable_tracker?
AIt would remain '' (empty string)
BIt would update to 'Failed to load data'
CIt would update to 'Not Found'
DIt would become undefined
💡 Hint
The catchError sets errorMessage on any HTTP error, not just 500, as shown in the execution_table.
Concept Snapshot
Angular HTTP error handling:
- Use catchError() in pipe() to catch errors.
- Set error message inside catchError.
- Return throwError() to rethrow error.
- Subscribe handles success or error.
- Keeps UI responsive and informative.
Full Transcript
This visual trace shows how Angular handles HTTP errors. First, an HTTP GET request is sent. When the response arrives, Angular checks if it is an error. If no error, data updates normally. If an error occurs, catchError runs, setting an error message and rethrowing the error. The subscription then receives either data or error. Variables like data and errorMessage update accordingly. This flow helps keep the app stable and user informed when requests fail.