Skip to content

Commit

Permalink
chore: Add error handling for API requests (#79)
Browse files Browse the repository at this point in the history
* chore: adding error handler display
* chore: bump version

---------

Signed-off-by: Iuri Pereira <[email protected]>
  • Loading branch information
iuricmp authored May 30, 2024
1 parent f6472ee commit 95cb3ea
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
15 changes: 4 additions & 11 deletions mobile/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
"expo": {
"name": "dsocial",
"slug": "dsocial",
"platforms": [
"ios",
"android"
],
"version": "0.2.0",
"platforms": ["ios", "android"],
"version": "1.0.2",
"orientation": "portrait",
"scheme": "tech.berty.dsocial",
"icon": "./assets/images/icon.png",
Expand All @@ -16,9 +13,7 @@
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"assetBundlePatterns": [
"**/*"
],
"assetBundlePatterns": ["**/*"],
"ios": {
"supportsTablet": true,
"bundleIdentifier": "tech.berty.dsocial.ios",
Expand Down Expand Up @@ -47,9 +42,7 @@
"experiments": {
"tsconfigPaths": true
},
"plugins": [
"expo-router"
],
"plugins": ["expo-router"],
"extra": {
"router": {
"origin": false
Expand Down
4 changes: 2 additions & 2 deletions mobile/app/home/feed.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ActivityIndicator, FlatList, Platform, StyleSheet, View } from "react-native";
import { ActivityIndicator, FlatList, Platform, StyleSheet, View, Alert } from "react-native";
import React, { useEffect, useRef, useState } from "react";
import { useNavigation, useRouter } from "expo-router";
import { useFeed } from "@gno/hooks/use-feed";
import Alert from "@gno/components/alert";
import Layout from "@gno/components/layout";
import useScrollToTop from "@gno/components/utils/useScrollToTopWithOffset";
import Button from "@gno/components/button";
Expand Down Expand Up @@ -31,6 +30,7 @@ export default function Page() {
const total = await feed.fetchCount();
setTotalPosts(total);
} catch (error) {
Alert.alert("Error while fetching posts.", " " + error);
console.error(error);
} finally {
setIsLoading(false);
Expand Down
3 changes: 2 additions & 1 deletion mobile/app/sign-up.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StyleSheet, Text, View, Button as RNButton, ScrollView, TextInput as RNTextInput } from "react-native";
import { StyleSheet, Text, View, Button as RNButton, ScrollView, TextInput as RNTextInput, Alert as RNAlert } from "react-native";
import React, { useEffect, useRef, useState } from "react";
import { router, useNavigation } from "expo-router";
import TextInput from "components/textinput";
Expand Down Expand Up @@ -74,6 +74,7 @@ export default function Page() {
await dispatch(loggedIn({ keyInfo: newAccount, bech32 }));
router.push("/home");
} catch (error) {
RNAlert.alert("Error", "" + error);
setError("" + error);
console.log(error);
} finally {
Expand Down
12 changes: 9 additions & 3 deletions mobile/src/hooks/use-feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useGnoNativeContext } from "@gnolang/gnonative";
import { useUserCache } from "./use-user-cache";
import useGnoJsonParser from "./use-gno-json-parser";
import { useIndexerContext } from "@gno/provider/indexer-provider";
import { Alert } from "react-native";

export const useFeed = () => {
const gno = useGnoNativeContext();
Expand All @@ -22,9 +23,14 @@ export const useFeed = () => {
async function fetchFeed(startIndex: number, endIndex: number): Promise<{ data: Post[]; n_posts: number }> {
const account = await checkActiveAccount();

const [nHomePosts, addrAndIDs] = await indexer.getHomePosts(account.address, BigInt(startIndex), BigInt(endIndex));
const result = await gno.qEval("gno.land/r/berty/social", `GetJsonTopPostsByID(${addrAndIDs})`);
return await enrichData(result, nHomePosts);
try {
const [nHomePosts, addrAndIDs] = await indexer.getHomePosts(account.address, BigInt(startIndex), BigInt(endIndex));
const result = await gno.qEval("gno.land/r/berty/social", `GetJsonTopPostsByID(${addrAndIDs})`);
return await enrichData(result, nHomePosts);
} catch (error) {
Alert.alert("Error while fetching posts", " " + error);
throw error;
}
}

async function enrichData(result: string, nHomePosts?: number) {
Expand Down
4 changes: 3 additions & 1 deletion mobile/src/hooks/use-onboarding.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Alert as RNAlert } from "react-native";
import { useGnoNativeContext } from "@gnolang/gnonative";

const useOnboarding = () => {
Expand Down Expand Up @@ -36,6 +37,7 @@ const useOnboarding = () => {
console.log("response: ", JSON.stringify(response));
}
} catch (error) {
RNAlert.alert("Erro on registering account", "" + error);
console.error("error registering account", error);
}
};
Expand Down Expand Up @@ -71,7 +73,7 @@ const useOnboarding = () => {
method: "POST",
headers: myHeaders,
body: raw,
reactNative: { textStreaming: true }
reactNative: { textStreaming: true },
};

const faucetRemote = process.env.EXPO_PUBLIC_FAUCET_REMOTE;
Expand Down

0 comments on commit 95cb3ea

Please sign in to comment.