Skip to content

Commit 73a8448

Browse files
committed
remove try catches from fetches functions to fail in data loader
1 parent 4d57049 commit 73a8448

File tree

5 files changed

+101
-145
lines changed

5 files changed

+101
-145
lines changed

src/lib/api/calendarEvents.ts

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,45 @@ import type {
33
ReqCommunityEvent,
44
} from "@/lib/interfaces"
55

6-
import { IS_DEV } from "@/lib/utils/env"
7-
86
export async function fetchCommunityEvents(): Promise<CommunityEventsReturnType> {
97
const apiKey = process.env.GOOGLE_API_KEY
108
const calendarId = process.env.GOOGLE_CALENDAR_ID
119

12-
try {
13-
const futureEventsReq = await fetch(
14-
`https://content.googleapis.com/calendar/v3/calendars/${calendarId}/events?key=${apiKey}&timeMin=${new Date().toISOString()}&maxResults=3&singleEvents=true&orderby=starttime`
15-
)
16-
const futureEvents = await futureEventsReq.json()
17-
const futureEventsReqData: ReqCommunityEvent[] = futureEvents.items
18-
19-
const pastEventsReq = await fetch(
20-
`https://content.googleapis.com/calendar/v3/calendars/${calendarId}/events?key=${apiKey}&timeMax=${new Date().toISOString()}&orderby=starttime`
21-
)
22-
const pastEvents = await pastEventsReq.json()
23-
const pastEventsReqData: ReqCommunityEvent[] = pastEvents.items
24-
25-
const pastEventData = pastEventsReqData
26-
.filter((event) => event.start)
27-
.slice(-4)
28-
.map((event) => {
29-
return {
30-
date: event.start.dateTime,
31-
title: event.summary,
32-
calendarLink: event.htmlLink,
33-
}
34-
})
35-
const upcomingEventData = futureEventsReqData
36-
.filter((event) => event.start)
37-
.reverse()
38-
.map((event) => {
39-
return {
40-
date: event.start.dateTime,
41-
title: event.summary,
42-
calendarLink: event.htmlLink,
43-
}
44-
})
45-
46-
return {
47-
pastEventData,
48-
upcomingEventData,
49-
}
50-
} catch (error) {
51-
// To improve DX, return empty arrays if we are in dev mode
52-
if (IS_DEV) {
53-
console.warn(
54-
"The community events fetch failed most probably because you are missing some env vars"
55-
)
56-
10+
const futureEventsReq = await fetch(
11+
`https://content.googleapis.com/calendar/v3/calendars/${calendarId}/events?key=${apiKey}&timeMin=${new Date().toISOString()}&maxResults=3&singleEvents=true&orderby=starttime`
12+
)
13+
const futureEvents = await futureEventsReq.json()
14+
const futureEventsReqData: ReqCommunityEvent[] = futureEvents.items
15+
16+
const pastEventsReq = await fetch(
17+
`https://content.googleapis.com/calendar/v3/calendars/${calendarId}/events?key=${apiKey}&timeMax=${new Date().toISOString()}&orderby=starttime`
18+
)
19+
const pastEvents = await pastEventsReq.json()
20+
const pastEventsReqData: ReqCommunityEvent[] = pastEvents.items
21+
22+
const pastEventData = pastEventsReqData
23+
.filter((event) => event.start)
24+
.slice(-4)
25+
.map((event) => {
26+
return {
27+
date: event.start.dateTime,
28+
title: event.summary,
29+
calendarLink: event.htmlLink,
30+
}
31+
})
32+
const upcomingEventData = futureEventsReqData
33+
.filter((event) => event.start)
34+
.reverse()
35+
.map((event) => {
5736
return {
58-
pastEventData: [],
59-
upcomingEventData: [],
37+
date: event.start.dateTime,
38+
title: event.summary,
39+
calendarLink: event.htmlLink,
6040
}
61-
}
41+
})
6242

63-
// In production mode, throw an error to stop the build in case this fetch fails
64-
console.error(error)
65-
throw new Error(
66-
"Something went wrong with requesting the calendar events data."
67-
)
43+
return {
44+
pastEventData,
45+
upcomingEventData,
6846
}
6947
}

src/lib/api/fetchEthPrice.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import { MetricReturnData } from "../types"
22

33
export const fetchEthPrice = async (): Promise<MetricReturnData> => {
4-
try {
5-
const data: { ethereum: { usd: number } } = await fetch(
6-
"https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd"
7-
).then((res) => res.json())
8-
const {
9-
ethereum: { usd },
10-
} = data
11-
if (!usd) throw new Error("Unable to fetch ETH price from CoinGecko")
12-
return { value: usd, timestamp: Date.now() }
13-
} catch (error: unknown) {
14-
console.error((error as Error).message)
15-
return { error: (error as Error).message }
16-
}
4+
const data: { ethereum: { usd: number } } = await fetch(
5+
"https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd"
6+
).then((res) => res.json())
7+
const {
8+
ethereum: { usd },
9+
} = data
10+
if (!usd) throw new Error("Unable to fetch ETH price from CoinGecko")
11+
return { value: usd, timestamp: Date.now() }
1712
}

src/lib/api/fetchGrowThePie.ts

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,56 +12,49 @@ const TXCOUNT = "txcount"
1212

1313
export const fetchGrowThePie = async (): Promise<GrowThePieData> => {
1414
const url = "https://api.growthepie.xyz/v1/fundamentals_full.json"
15-
try {
16-
const response = await fetch(url)
17-
if (!response.ok) {
18-
console.log(response.status, response.statusText)
19-
throw new Error("Failed to fetch GrowThePie data")
20-
}
21-
const data: DataItem[] = await response.json()
2215

23-
const mostRecentDate = data.reduce((latest, item) => {
24-
const itemDate = new Date(item.date)
25-
return itemDate > new Date(latest) ? item.date : latest
26-
}, data[0].date)
16+
const response = await fetch(url)
17+
if (!response.ok) {
18+
console.log(response.status, response.statusText)
19+
throw new Error("Failed to fetch GrowThePie data")
20+
}
21+
const data: DataItem[] = await response.json()
2722

28-
const mostRecentData = data.filter(
29-
(item) =>
30-
item.date === mostRecentDate &&
31-
[TXCOSTS_MEDIAN_USD, TXCOUNT].includes(item.metric_key)
32-
)
23+
const mostRecentDate = data.reduce((latest, item) => {
24+
const itemDate = new Date(item.date)
25+
return itemDate > new Date(latest) ? item.date : latest
26+
}, data[0].date)
3327

34-
let totalTxCount = 0
35-
let weightedSum = 0
28+
const mostRecentData = data.filter(
29+
(item) =>
30+
item.date === mostRecentDate &&
31+
[TXCOSTS_MEDIAN_USD, TXCOUNT].includes(item.metric_key)
32+
)
3633

37-
mostRecentData.forEach((item) => {
38-
if (item.metric_key !== TXCOSTS_MEDIAN_USD) return
34+
let totalTxCount = 0
35+
let weightedSum = 0
3936

40-
const txCountItem = mostRecentData.find(
41-
(txItem) =>
42-
txItem.metric_key === TXCOUNT && txItem.origin_key === item.origin_key
43-
)
44-
if (!txCountItem) return
37+
mostRecentData.forEach((item) => {
38+
if (item.metric_key !== TXCOSTS_MEDIAN_USD) return
39+
40+
const txCountItem = mostRecentData.find(
41+
(txItem) =>
42+
txItem.metric_key === TXCOUNT && txItem.origin_key === item.origin_key
43+
)
44+
if (!txCountItem) return
4545

46-
totalTxCount += txCountItem.value
47-
weightedSum += item.value * txCountItem.value
48-
})
46+
totalTxCount += txCountItem.value
47+
weightedSum += item.value * txCountItem.value
48+
})
4949

50-
// The weighted average of txcosts_median_usd, by txcount on each network (origin_key)
51-
const weightedAverage = totalTxCount ? weightedSum / totalTxCount : 0
50+
// The weighted average of txcosts_median_usd, by txcount on each network (origin_key)
51+
const weightedAverage = totalTxCount ? weightedSum / totalTxCount : 0
5252

53-
// Last updated timestamp
54-
const timestamp = Date.now()
53+
// Last updated timestamp
54+
const timestamp = Date.now()
5555

56-
return {
57-
txCount: { value: totalTxCount, timestamp },
58-
txCostsMedianUsd: { value: weightedAverage, timestamp },
59-
}
60-
} catch (error: unknown) {
61-
console.error((error as Error).message)
62-
return {
63-
txCount: { error: (error as Error).message },
64-
txCostsMedianUsd: { error: (error as Error).message },
65-
}
56+
return {
57+
txCount: { value: totalTxCount, timestamp },
58+
txCostsMedianUsd: { value: weightedAverage, timestamp },
6659
}
6760
}

src/lib/api/fetchTotalEthStaked.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,21 @@ export const fetchTotalEthStaked = async (): Promise<MetricReturnData> => {
1212

1313
const url = new URL("api/v1/query/3915587/results", DUNE_API_URL)
1414

15-
try {
16-
const response = await fetch(url, {
17-
headers: { "X-Dune-API-Key": DUNE_API_KEY },
18-
})
19-
if (!response.ok) {
20-
console.log(response.status, response.statusText)
21-
throw new Error("Failed to fetch eth staked data")
22-
}
15+
const response = await fetch(url, {
16+
headers: { "X-Dune-API-Key": DUNE_API_KEY },
17+
})
18+
if (!response.ok) {
19+
console.log(response.status, response.statusText)
20+
throw new Error("Failed to fetch eth staked data")
21+
}
2322

24-
const json: EthStakedResponse = await response.json()
25-
const {
26-
result: { rows = [] },
27-
} = json
28-
// Today's value at start of array
29-
const value = rows[0].cum_deposited_eth
23+
const json: EthStakedResponse = await response.json()
24+
const {
25+
result: { rows = [] },
26+
} = json
27+
// Today's value at start of array
28+
const value = rows[0].cum_deposited_eth
3029

31-
// current value (number, unformatted)
32-
return { value, timestamp: Date.now() }
33-
} catch (error: unknown) {
34-
console.error((error as Error).message)
35-
return { error: (error as Error).message }
36-
}
30+
// current value (number, unformatted)
31+
return { value, timestamp: Date.now() }
3732
}

src/lib/api/fetchTotalValueLocked.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import { DefiLlamaTVLResponse, MetricReturnData } from "@/lib/types"
22

33
export const fetchTotalValueLocked = async (): Promise<MetricReturnData> => {
4-
try {
5-
const response = await fetch(`https://api.llama.fi/charts/Ethereum`)
6-
if (!response.ok) {
7-
console.log(response.status, response.statusText)
8-
throw new Error("Failed to fetch Defi Llama TVL data")
9-
}
4+
const response = await fetch(`https://api.llama.fi/charts/Ethereum`)
5+
if (!response.ok) {
6+
console.log(response.status, response.statusText)
7+
throw new Error("Failed to fetch Defi Llama TVL data")
8+
}
109

11-
const json: DefiLlamaTVLResponse = await response.json()
12-
// Today's value at end of array
13-
const value = json[json.length - 1].totalLiquidityUSD
10+
const json: DefiLlamaTVLResponse = await response.json()
11+
// Today's value at end of array
12+
const value = json[json.length - 1].totalLiquidityUSD
1413

15-
// current value (number, unformatted)
16-
return { value, timestamp: Date.now() }
17-
} catch (error: unknown) {
18-
console.error((error as Error).message)
19-
return { error: (error as Error).message }
20-
}
14+
// current value (number, unformatted)
15+
return { value, timestamp: Date.now() }
2116
}

0 commit comments

Comments
 (0)