fetch API or XMLHTTPRequest: How to get exact error reason for a network error?












0















How to retrieve exact information about a network error? Chrome browser prints things like "net::ERR_CONNECTION_TIMED_OUT" or "net::ERR_CERT_INVALID" into console but how can I retrieve them via JS? Is that possible to get them at all? Currently I use fetch API but I can switch to XMLHTTPRequest easily.



I need that to handle such errors to inform a user and let him know what does he need to do.










share|improve this question























  • The fetch API use promises so you can easily get the errors with .catch(). It is what you are looking for ?

    – Arkellys
    Nov 13 '18 at 13:13













  • Nope. The error doesn't contain any specific information. It just says "TypeError: Failed to fetch"

    – Andrey Gubanov
    Nov 13 '18 at 13:16











  • You want to check the response status. The invalid cert for example should be a 400 series. Take a look at wretch wrapper for fetch(). Will simplify this for you

    – charlietfl
    Nov 13 '18 at 13:20













  • I don't want to check response status. I want to check "reason of failed request", where failed request doesn't have any code.

    – Andrey Gubanov
    Nov 13 '18 at 13:31











  • @charlietfl — It won't be a 400 because it will error at the TLS level not the HTTP level.

    – Quentin
    Nov 13 '18 at 13:35
















0















How to retrieve exact information about a network error? Chrome browser prints things like "net::ERR_CONNECTION_TIMED_OUT" or "net::ERR_CERT_INVALID" into console but how can I retrieve them via JS? Is that possible to get them at all? Currently I use fetch API but I can switch to XMLHTTPRequest easily.



I need that to handle such errors to inform a user and let him know what does he need to do.










share|improve this question























  • The fetch API use promises so you can easily get the errors with .catch(). It is what you are looking for ?

    – Arkellys
    Nov 13 '18 at 13:13













  • Nope. The error doesn't contain any specific information. It just says "TypeError: Failed to fetch"

    – Andrey Gubanov
    Nov 13 '18 at 13:16











  • You want to check the response status. The invalid cert for example should be a 400 series. Take a look at wretch wrapper for fetch(). Will simplify this for you

    – charlietfl
    Nov 13 '18 at 13:20













  • I don't want to check response status. I want to check "reason of failed request", where failed request doesn't have any code.

    – Andrey Gubanov
    Nov 13 '18 at 13:31











  • @charlietfl — It won't be a 400 because it will error at the TLS level not the HTTP level.

    – Quentin
    Nov 13 '18 at 13:35














0












0








0








How to retrieve exact information about a network error? Chrome browser prints things like "net::ERR_CONNECTION_TIMED_OUT" or "net::ERR_CERT_INVALID" into console but how can I retrieve them via JS? Is that possible to get them at all? Currently I use fetch API but I can switch to XMLHTTPRequest easily.



I need that to handle such errors to inform a user and let him know what does he need to do.










share|improve this question














How to retrieve exact information about a network error? Chrome browser prints things like "net::ERR_CONNECTION_TIMED_OUT" or "net::ERR_CERT_INVALID" into console but how can I retrieve them via JS? Is that possible to get them at all? Currently I use fetch API but I can switch to XMLHTTPRequest easily.



I need that to handle such errors to inform a user and let him know what does he need to do.







javascript xmlhttprequest fetch






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 13:10









Andrey GubanovAndrey Gubanov

112




112













  • The fetch API use promises so you can easily get the errors with .catch(). It is what you are looking for ?

    – Arkellys
    Nov 13 '18 at 13:13













  • Nope. The error doesn't contain any specific information. It just says "TypeError: Failed to fetch"

    – Andrey Gubanov
    Nov 13 '18 at 13:16











  • You want to check the response status. The invalid cert for example should be a 400 series. Take a look at wretch wrapper for fetch(). Will simplify this for you

    – charlietfl
    Nov 13 '18 at 13:20













  • I don't want to check response status. I want to check "reason of failed request", where failed request doesn't have any code.

    – Andrey Gubanov
    Nov 13 '18 at 13:31











  • @charlietfl — It won't be a 400 because it will error at the TLS level not the HTTP level.

    – Quentin
    Nov 13 '18 at 13:35



















  • The fetch API use promises so you can easily get the errors with .catch(). It is what you are looking for ?

    – Arkellys
    Nov 13 '18 at 13:13













  • Nope. The error doesn't contain any specific information. It just says "TypeError: Failed to fetch"

    – Andrey Gubanov
    Nov 13 '18 at 13:16











  • You want to check the response status. The invalid cert for example should be a 400 series. Take a look at wretch wrapper for fetch(). Will simplify this for you

    – charlietfl
    Nov 13 '18 at 13:20













  • I don't want to check response status. I want to check "reason of failed request", where failed request doesn't have any code.

    – Andrey Gubanov
    Nov 13 '18 at 13:31











  • @charlietfl — It won't be a 400 because it will error at the TLS level not the HTTP level.

    – Quentin
    Nov 13 '18 at 13:35

















The fetch API use promises so you can easily get the errors with .catch(). It is what you are looking for ?

– Arkellys
Nov 13 '18 at 13:13







The fetch API use promises so you can easily get the errors with .catch(). It is what you are looking for ?

– Arkellys
Nov 13 '18 at 13:13















Nope. The error doesn't contain any specific information. It just says "TypeError: Failed to fetch"

– Andrey Gubanov
Nov 13 '18 at 13:16





Nope. The error doesn't contain any specific information. It just says "TypeError: Failed to fetch"

– Andrey Gubanov
Nov 13 '18 at 13:16













You want to check the response status. The invalid cert for example should be a 400 series. Take a look at wretch wrapper for fetch(). Will simplify this for you

– charlietfl
Nov 13 '18 at 13:20







You want to check the response status. The invalid cert for example should be a 400 series. Take a look at wretch wrapper for fetch(). Will simplify this for you

– charlietfl
Nov 13 '18 at 13:20















I don't want to check response status. I want to check "reason of failed request", where failed request doesn't have any code.

– Andrey Gubanov
Nov 13 '18 at 13:31





I don't want to check response status. I want to check "reason of failed request", where failed request doesn't have any code.

– Andrey Gubanov
Nov 13 '18 at 13:31













@charlietfl — It won't be a 400 because it will error at the TLS level not the HTTP level.

– Quentin
Nov 13 '18 at 13:35





@charlietfl — It won't be a 400 because it will error at the TLS level not the HTTP level.

– Quentin
Nov 13 '18 at 13:35












1 Answer
1






active

oldest

votes


















0














you can get the exact error as follow:



     fetch('url',
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify({data})
})
.then(function(response) {
console.log(response);
//handle here or in catch
})
.then(function(json){
console.log("succeed json re");
});


You'll get resolved response once your promise has been resolved.



Response.status will give you status code.
Response.statusText will give you exact error description as ERR_CONNECTION_TIMED_OUT. which will be in case of errors and then response.ok will be false.



For more info: FETCH






share|improve this answer
























  • "Response.statusText — A string (default value "OK"), which corresponds to the HTTP status code message." 1. ERR_CONNECTION_TIMED_OUT doesn't have any status code as I know. 2. When ERR_CONNECTION_TIMED_OUT happens, fetch API throws an error which means there is no way to get Response instance and read statusText property.

    – Andrey Gubanov
    Nov 13 '18 at 15:25













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281740%2ffetch-api-or-xmlhttprequest-how-to-get-exact-error-reason-for-a-network-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














you can get the exact error as follow:



     fetch('url',
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify({data})
})
.then(function(response) {
console.log(response);
//handle here or in catch
})
.then(function(json){
console.log("succeed json re");
});


You'll get resolved response once your promise has been resolved.



Response.status will give you status code.
Response.statusText will give you exact error description as ERR_CONNECTION_TIMED_OUT. which will be in case of errors and then response.ok will be false.



For more info: FETCH






share|improve this answer
























  • "Response.statusText — A string (default value "OK"), which corresponds to the HTTP status code message." 1. ERR_CONNECTION_TIMED_OUT doesn't have any status code as I know. 2. When ERR_CONNECTION_TIMED_OUT happens, fetch API throws an error which means there is no way to get Response instance and read statusText property.

    – Andrey Gubanov
    Nov 13 '18 at 15:25


















0














you can get the exact error as follow:



     fetch('url',
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify({data})
})
.then(function(response) {
console.log(response);
//handle here or in catch
})
.then(function(json){
console.log("succeed json re");
});


You'll get resolved response once your promise has been resolved.



Response.status will give you status code.
Response.statusText will give you exact error description as ERR_CONNECTION_TIMED_OUT. which will be in case of errors and then response.ok will be false.



For more info: FETCH






share|improve this answer
























  • "Response.statusText — A string (default value "OK"), which corresponds to the HTTP status code message." 1. ERR_CONNECTION_TIMED_OUT doesn't have any status code as I know. 2. When ERR_CONNECTION_TIMED_OUT happens, fetch API throws an error which means there is no way to get Response instance and read statusText property.

    – Andrey Gubanov
    Nov 13 '18 at 15:25
















0












0








0







you can get the exact error as follow:



     fetch('url',
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify({data})
})
.then(function(response) {
console.log(response);
//handle here or in catch
})
.then(function(json){
console.log("succeed json re");
});


You'll get resolved response once your promise has been resolved.



Response.status will give you status code.
Response.statusText will give you exact error description as ERR_CONNECTION_TIMED_OUT. which will be in case of errors and then response.ok will be false.



For more info: FETCH






share|improve this answer













you can get the exact error as follow:



     fetch('url',
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify({data})
})
.then(function(response) {
console.log(response);
//handle here or in catch
})
.then(function(json){
console.log("succeed json re");
});


You'll get resolved response once your promise has been resolved.



Response.status will give you status code.
Response.statusText will give you exact error description as ERR_CONNECTION_TIMED_OUT. which will be in case of errors and then response.ok will be false.



For more info: FETCH







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 14:45









Krina SoniKrina Soni

34515




34515













  • "Response.statusText — A string (default value "OK"), which corresponds to the HTTP status code message." 1. ERR_CONNECTION_TIMED_OUT doesn't have any status code as I know. 2. When ERR_CONNECTION_TIMED_OUT happens, fetch API throws an error which means there is no way to get Response instance and read statusText property.

    – Andrey Gubanov
    Nov 13 '18 at 15:25





















  • "Response.statusText — A string (default value "OK"), which corresponds to the HTTP status code message." 1. ERR_CONNECTION_TIMED_OUT doesn't have any status code as I know. 2. When ERR_CONNECTION_TIMED_OUT happens, fetch API throws an error which means there is no way to get Response instance and read statusText property.

    – Andrey Gubanov
    Nov 13 '18 at 15:25



















"Response.statusText — A string (default value "OK"), which corresponds to the HTTP status code message." 1. ERR_CONNECTION_TIMED_OUT doesn't have any status code as I know. 2. When ERR_CONNECTION_TIMED_OUT happens, fetch API throws an error which means there is no way to get Response instance and read statusText property.

– Andrey Gubanov
Nov 13 '18 at 15:25







"Response.statusText — A string (default value "OK"), which corresponds to the HTTP status code message." 1. ERR_CONNECTION_TIMED_OUT doesn't have any status code as I know. 2. When ERR_CONNECTION_TIMED_OUT happens, fetch API throws an error which means there is no way to get Response instance and read statusText property.

– Andrey Gubanov
Nov 13 '18 at 15:25




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281740%2ffetch-api-or-xmlhttprequest-how-to-get-exact-error-reason-for-a-network-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation