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.

Is there a way to catch a 401 error code? I first thought I could add it to the checkStatus function but it seems that a 401 does reject the promise.

But there is little to no information in the error object. https://cldup.com/R8VE0alk2z.png

Am I missing something?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:24 (6 by maintainers)

github_iconTop GitHub Comments

83reactions
racc-oo-ncommented, Jun 13, 2017

For anyone who will have this issue in the future

Fetch API fails only if it can’t make a request. And if it can, fetch will be executed successfully even if it has a bad status. For example:

fetch('http://google.com')
  .then(response => {
    // response from server
    // here you can check status of response and handle it manually
    switch (response.status) {
      case 500: console.error('Some server error'); break;
      case 401: console.error('Unauthorized'); break;
      // ...
    }
    // or you can check if status in the range 200 to 299
    if (response.ok) {
      return response;
    } else {
      // push error further for the next `catch`, like
      return Promise.reject(response);
      // or another way
      throw Error(response.statusText);
    }
  })
  .catch(error => {
    // here you will get only Fetch API errors and those you threw or rejected above
    // in most cases Fetch API error will look like common Error object
    // {
    //   name: "TypeError",
    //   message: "Failed to fetch",
    //   stack: ...
    // }
  });

And here are some (not all) of the errors from Fetch API:

  • misspelled url like fetch('https::://hey.com')TypeError Failed to execute 'fetch' on 'Window': Failed to parse URL from https::://hey.com;
  • nonexistent url like fetch('http://hey')TypeError: Failed to fetch (GET http://hey/ net::ERR_NAME_NOT_RESOLVED);
  • you don’t have an internet connection fetch('https://google.com')TypeError: Failed to fetch (GET https://google.com/ net::ERR_NAME_RESOLUTION_FAILED)
  • because of the Content Security Policy fetch('https://google.com')TypeError: Failed to fetch (Refused to connect to 'https://google.com/' because it violates the following Content Security Policy directive: "connect-src 'self'...)
  • because of the CORS fetch('https://google.com')TypeError: Failed to fetch (Fetch API cannot load https://google.com/ has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource....)

The last two cases you can fix by adding (or editing) the appropriate headers on the server, if you have an access.

15reactions
vdclouiscommented, Sep 18, 2015

@dgraham that’s not correct. Fetch seems to reject the promise on a 401. So you don’t have access to the response object

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to catch 401 error using fetch method of javascript
1. Try to get the property error. · 3. If I understand correctly from the docs, an error will not be thrown if...
Read more >
How to Quickly Fix the 401 Unauthorized Error (5 Methods)
The 401 (Unauthorized) status code indicates that the request has not been applied because it lacks valid authentication credentials for the ...
Read more >
401 Unauthorized - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 401 Unauthorized response status code indicates that the client request has not been completed ...
Read more >
Angular — How to intercept 401 err response and redirect to ...
Below is an example of listening to an http response which was sent by the app to the api and returned with status...
Read more >
401 Error: 5 Ways to Troubleshoot and Fix It - Hostinger
The 401 Unauthorized error is triggered by unauthenticated requests made to a WordPress web server. Learn how to identify and fix the issue....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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