Skip to content
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

feat: add network error and timeout toast #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
package-lock=false
legacy-peer-deps=true
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
"@types/react-redux": "7.0.0",
"@types/use-subscription": "^1.0.0",
"@yuyongmao/react-native-chart-kit": "^1.0.0",
"apollo-link-timeout": "^4.0.0",
"axios": "^0.21.1",
"buffer": "^6.0.3",
"currency-icons": "^1.0.14",
Expand Down Expand Up @@ -170,8 +171,7 @@
"redux-persist": "^5.10.0",
"redux-thunk": "2.3.0",
"sentry-expo": "^2.1.2",
"short-number": "^1.0.7",
"ts-invariant": "^0.7.0"
"short-number": "^1.0.7"
},
"devDependencies": {
"@puncsky/eslint-config-onefx": "^2.3.0",
Expand Down
4 changes: 4 additions & 0 deletions src/common/apollo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import fetch from "isomorphic-unfetch";
import { getEndpoint } from "@/common/request";
import { store } from "@/common/store";
import { onErrorLink } from "@/common/apollo-error-handling";
import ApolloLinkTimeout from "apollo-link-timeout";

const timeoutLink = new ApolloLinkTimeout(10000);

const middlewareLink = new ApolloLink((operation, forward) => {
operation.setContext({
Expand All @@ -21,6 +24,7 @@ const middlewareLink = new ApolloLink((operation, forward) => {
const link = middlewareLink.concat(
ApolloLink.from([
onErrorLink,
timeoutLink,
new HttpLink({
uri: getEndpoint("api-gateway/"),
fetch,
Expand Down
11 changes: 9 additions & 2 deletions src/common/apollo-error-handling.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { onError } from "@apollo/client/link/error";
import { store } from "@/common/store";
import { Toast } from "@ant-design/react-native";
import { i18n } from "@/translations";

export const onErrorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
Expand All @@ -18,7 +20,12 @@ export const onErrorLink = onError(({ graphQLErrors, networkError }) => {
}
}
if (networkError) {
// eslint-disable-next-line no-console
console.log(`[Network error]: ${networkError}`);
if (networkError.message.indexOf("Network request failed") >= 0) {
Toast.info(i18n.t("netError"));
} else if (networkError.message.indexOf("Timeout") >= 0) {
Toast.info(i18n.t("netTimeout"));
} else {
Toast.info(i18n.t("serverError"));
}
}
});
3 changes: 3 additions & 0 deletions src/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,7 @@ export const en = {
"10": "OCT",
"11": "NOV",
"12": "DEC",
netTimeout: "Network request timeout",
netError: "Network request failed",
serverError: "Server error",
};
3 changes: 3 additions & 0 deletions src/translations/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,7 @@ export const zh = {
"10": "十月",
"11": "十一月",
"12": "十二月",
netTimeout: "网络请求超时",
netError: "网络请求失败",
serverError: "服务器错误",
};