question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong error type in httpclient doc

See original GitHub issue

📚 Docs or angular.io bug report

Description

A type was changed in a recent commit in angular which is not yet represented in the docs.

What’s the affected URL?**

https://angular.io/guide/http#getting-error-details

Reproduction Steps**

In the code snippet app/config/config.service.ts there is this line:

if (error.error instanceof ErrorEvent)

In case of a network error (or similar like a CORS error) the error property of the HttpErrorResponse is should be according to the docs an ErrorEvent. But in my tests always seems to be a ProgressEvent.

In this recent commit https://github.com/angular/angular/commit/0139b11227fdd93983a8c6bd870d579d798fa244#diff-26683cfb34fa97883213282055aef1e7 in the file packages/common/http/src/xhr.ts the type of the error property of the returned HttpErrorResponse was changed from ErrorEvent to ProgressEvent.

Unfortunately unlike ErrorEvent the ProgressEvent has no message property. It is only usable as a indicator for a network error. The message property of the HttpErrorResponse itself has to be queried to retrieve a human readable error message.

The behaviour can be tested in https://stackblitz.com/edit/error-handling-httpclient?file=src%2Fapp%2Fuser.service.ts

I am using Angular 8.2.14

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
devversioncommented, Mar 16, 2020

@wildsalarian Thanks for this issue. I think this has always been an issue because the HttpClient never emitted an ErrorEvent, but instead always used ProgressEvent. It was just incorrectly assumed that ErrorEvent will be returned (which has been flagged as incorrect by TypeScript in 0139b11)

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/error_event describes error is of type ProgressEvent, so I think we should change:

  1. Documentation to use ProgressEvent in the instanceof check (seen in this guide)
  2. Change TestRequest to return ProgressEvent instead of ErrorEvent (which is incorrect).

This is an interesting question: https://stackoverflow.com/questions/51451508/why-is-a-progressevent-object-passed-to-a-javascript-ajax-error-event-handler.

1reaction
cexbrayatcommented, Dec 13, 2021

@devversion Now that #36082 was merged, this can probably be closed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

HttpClient exception handling guide
HttpException represents an abstract logical error in HttpClient. Generally this kind of exception cannot be automatically recovered from. org.apache.commons.
Read more >
Angular - Defining error type to HttpClient.post() - Stack Overflow
1 Answer 1 · I have already tried this. The problem is that you can use any in your error. I want that...
Read more >
You're using HttpClient wrong and it is destabilizing your ...
My site was unstable and my clients furious, with a simple fix performance improved greatly and the instability disapeared.
Read more >
Handling Exceptions Using the Angular HttpClient Service
Re-throwing HTTP Exceptions ; throwError . First, update your BooksService to throw a custom error depending on the final status of the HTTP ......
Read more >
HTTP client error status code checklist - Peter Hilton
'Bad request' doesn't tell clients anything about what they did wrong, so this status code relies on application-specific error details in the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found