-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Catch a 401 #201
Comments
Curious, Fetch shouldn't reject the promise on HTTP errors, maybe edit your issue and paste the code you're using? |
I should have mentioned that I'm using Chrome 47. So if I understand correctly it actually doesn't use the fallback but just the standard spec. So it seems there's some issue there... |
The polyfill is not active in browsers that include a native implementation of Here's how you can detect and handle a 401 status code in the response from the server. fetch('/').then(function(response) {
if (response.status === 401) {
// 401 returned from server
} else {
// another status code
}
}) |
@dgraham that's not correct. Fetch seems to reject the promise on a 401. So you don't have access to the response object |
@vdclouis I've just tested in Chrome 45 latest stable, and it does resolve the promise on 401. So the behavior you're seeing in Chrome 47 prerelease with rejecting the promise might be a regression and I bet the Chromium dev team would like to know about that. |
@vdclouis Actually if I try to replicate your experience on Chrome Canary 47, I still can't. It behaves as expected. I added the test('resolves promise on 401 error', function() {
return fetch('/authfail').then(function(response) {
assert.equal(response.status, 401)
assert.equal(response.ok, false)
return response.text()
}).then(function(body) {
assert.equal(body, 'you need to auth')
})
}) but it passes. |
Hmm, maybe because I'm sending credentials and thus does a Options call first? |
Is it a cross-site request? |
Yes, yes it is. |
The promise is supposed to be rejected if CORS doesn't allow it. I just tested with Chrome 45 and Chrome 47 and the promise is rejected by default when "No 'Access-Control-Allow-Origin' header is present on the requested resource." However if I add the |
Hmm, If this is a server setting I'm afraid I'm stuck... |
Cors headers were not set when there was no security context (401) or authorizatin issue (403). This was a pb because Chrome didn't call fetch.then(xxx), but instead fetch.catch(xxx) and we didn't have access to statusCode in catch. See : * spring-projects/spring-boot#5834 * JakeChampion/fetch#201
@vdclouis Hey Louis, was you able to find a way to detect 401 code for rejected by CORS request? |
@NeXTs any news? Could you get it to work? |
I am seeing the same. |
I had to get the latest systemjs src and all was well. SystemJS v0.19.47 did the trick for me. |
For anyone who will have this issue in the futureFetch API fails only if it can't make a request. And if it can, 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:
The last two cases you can fix by adding (or editing) the appropriate headers on the server, if you have an access. |
Is there a way to get those additional error messages to determine which error is occurring? For example (in Chrome 67), I'm running the following code: fetch('https://fail').then(function(response){
console.log('Fetch was successful', response);
}).catch(function(event){
console.log('Fetch failed', event);
}); Which fails, but the only message in the console is Edit: Thanks for the info below- in my situation |
@chrisblakley You can inspect If there is no |
This comment has been minimized.
This comment has been minimized.
going off that, i know it is not a network error 😂 but i also know my api server is running properly and there are no auth problems, because i checked Okta |
@mislav got the core of the problem here I believe. Fetch response promise is not rejected because of 401 status code but because 401 responses don't have the proper CORS headers. In my case (Laravel), I had to add them manually on 401 error responses: // app/Exceptions/Handler.php
protected function prepareJsonResponse($request, AuthenticationException $e)
{
// ...
$response = response()->json($data, $statusCode);
$cors = app(Barryvdh\Cors\Stack\CorsService::class);
if ($cors->isCorsRequest($request)) {
$response = $cors->addActualRequestHeaders($response, $request);
}
return $response;
} |
either way, this spec desperately needs to be fixed |
I'd say it works as expected, there is nothing to be fixed. |
Is there a way to catch a 401 error code? I first thought I could add it to the
checkStatus
function butit seems that a 401 does reject the promise.
But there is little to no information in the error object.
Am I missing something?
The text was updated successfully, but these errors were encountered: