admin管理员组文章数量:1025202
I have a working API to get items from the server as below. I am using React to use this data. Now, I want to catch all server errors that begins with 5__ and display a message like "No connection with internet" or something like that.
export const GetItems = (operand, searchValue) => {
const trimmedValue = searchValue.trim();
let binedResults;
// make 2 API calls to search on both item_name and code;
// then bine them;
// there is no API method to do this, that I could find
return getItemsByName(operand, trimmedValue)
.then(result => (
(binedResults = [].concat(result))
))
.then(() => getItemsByCode(operand, trimmedValue))
.then(result => (
(binedResults = binedResults.concat(result))
));
};
Currently, I need to look at the console to check if there is a problem with connection.
Updated as @Dane requested
const getItemsByCode = (operand, searchValue) => (
FetchToJson(BuildCodeSearchUrl(operand, searchValue))
);
It's just calling a method to build the URL. You can consider that everything is working good, getting the response if there is a connection.
I have a working API to get items from the server as below. I am using React to use this data. Now, I want to catch all server errors that begins with 5__ and display a message like "No connection with internet" or something like that.
export const GetItems = (operand, searchValue) => {
const trimmedValue = searchValue.trim();
let binedResults;
// make 2 API calls to search on both item_name and code;
// then bine them;
// there is no API method to do this, that I could find
return getItemsByName(operand, trimmedValue)
.then(result => (
(binedResults = [].concat(result))
))
.then(() => getItemsByCode(operand, trimmedValue))
.then(result => (
(binedResults = binedResults.concat(result))
));
};
Currently, I need to look at the console to check if there is a problem with connection.
Updated as @Dane requested
const getItemsByCode = (operand, searchValue) => (
FetchToJson(BuildCodeSearchUrl(operand, searchValue))
);
It's just calling a method to build the URL. You can consider that everything is working good, getting the response if there is a connection.
Share Improve this question edited Nov 27, 2017 at 6:54 psuresh asked Nov 27, 2017 at 6:08 psureshpsuresh 5842 gold badges14 silver badges27 bronze badges 2-
Can you please add the code for
getItemsByCode
as well ? – Dane Commented Nov 27, 2017 at 6:45 - wat http library are you using ? – Panther Commented Nov 27, 2017 at 7:39
1 Answer
Reset to default 4Use catch()
:
return getItemsByName(operand, trimmedValue)
.then(result => (
(binedResults = [].concat(result))
))
.then(() => getItemsByCode(operand, trimmedValue))
.then(result => (
(binedResults = binedResults.concat(result))
))
.catch((error) => {
if (error.response) { // if there is response, it means its not a 50x, but 4xx
} else { // gets activated on 50x errors, since no response from server
// do whatever you want here :)
}
});
I have a working API to get items from the server as below. I am using React to use this data. Now, I want to catch all server errors that begins with 5__ and display a message like "No connection with internet" or something like that.
export const GetItems = (operand, searchValue) => {
const trimmedValue = searchValue.trim();
let binedResults;
// make 2 API calls to search on both item_name and code;
// then bine them;
// there is no API method to do this, that I could find
return getItemsByName(operand, trimmedValue)
.then(result => (
(binedResults = [].concat(result))
))
.then(() => getItemsByCode(operand, trimmedValue))
.then(result => (
(binedResults = binedResults.concat(result))
));
};
Currently, I need to look at the console to check if there is a problem with connection.
Updated as @Dane requested
const getItemsByCode = (operand, searchValue) => (
FetchToJson(BuildCodeSearchUrl(operand, searchValue))
);
It's just calling a method to build the URL. You can consider that everything is working good, getting the response if there is a connection.
I have a working API to get items from the server as below. I am using React to use this data. Now, I want to catch all server errors that begins with 5__ and display a message like "No connection with internet" or something like that.
export const GetItems = (operand, searchValue) => {
const trimmedValue = searchValue.trim();
let binedResults;
// make 2 API calls to search on both item_name and code;
// then bine them;
// there is no API method to do this, that I could find
return getItemsByName(operand, trimmedValue)
.then(result => (
(binedResults = [].concat(result))
))
.then(() => getItemsByCode(operand, trimmedValue))
.then(result => (
(binedResults = binedResults.concat(result))
));
};
Currently, I need to look at the console to check if there is a problem with connection.
Updated as @Dane requested
const getItemsByCode = (operand, searchValue) => (
FetchToJson(BuildCodeSearchUrl(operand, searchValue))
);
It's just calling a method to build the URL. You can consider that everything is working good, getting the response if there is a connection.
Share Improve this question edited Nov 27, 2017 at 6:54 psuresh asked Nov 27, 2017 at 6:08 psureshpsuresh 5842 gold badges14 silver badges27 bronze badges 2-
Can you please add the code for
getItemsByCode
as well ? – Dane Commented Nov 27, 2017 at 6:45 - wat http library are you using ? – Panther Commented Nov 27, 2017 at 7:39
1 Answer
Reset to default 4Use catch()
:
return getItemsByName(operand, trimmedValue)
.then(result => (
(binedResults = [].concat(result))
))
.then(() => getItemsByCode(operand, trimmedValue))
.then(result => (
(binedResults = binedResults.concat(result))
))
.catch((error) => {
if (error.response) { // if there is response, it means its not a 50x, but 4xx
} else { // gets activated on 50x errors, since no response from server
// do whatever you want here :)
}
});
本文标签: reactjsCatch 504 (Gateway Timeout) Error in ReactJavaScriptStack Overflow
版权声明:本文标题:reactjs - Catch 504 (Gateway Timeout) Error in React, JavaScript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1741897798a1894092.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论