Skip to content

Commit 4d1f445

Browse files
committed
add logs to failed responses
1 parent 2be6e65 commit 4d1f445

File tree

7 files changed

+120
-49
lines changed

7 files changed

+120
-49
lines changed

src/lib/api/calendarEvents.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,27 @@ export async function fetchCommunityEvents(): Promise<CommunityEventsReturnType>
1010
const calendarId = process.env.GOOGLE_CALENDAR_ID
1111

1212
try {
13-
const futureEventsReq: ReqCommunityEvent[] = await fetch(
13+
const futureEventsReq = await fetch(
1414
`https://content.googleapis.com/calendar/v3/calendars/${calendarId}/events?key=${apiKey}&timeMin=${new Date().toISOString()}&maxResults=3`
15-
).then(response => response.json())
16-
.then(data => data.items)
15+
)
16+
const futureEvents = await futureEventsReq.json()
17+
const futureEventsReqData: ReqCommunityEvent[] = futureEvents.items
1718

18-
const pastEventsReq: ReqCommunityEvent[] = await fetch(
19+
const pastEventsReq = await fetch(
1920
`https://content.googleapis.com/calendar/v3/calendars/${calendarId}/events?key=${apiKey}&timeMax=${new Date().toISOString()}&maxResults=4`
20-
).then(response => response.json())
21-
.then(data => data.items)
21+
)
22+
const pastEvents = await pastEventsReq.json()
23+
const pastEventsReqData: ReqCommunityEvent[] = pastEvents.items
2224

23-
const pastEventData = pastEventsReq.map((event) => {
25+
const pastEventData = pastEventsReqData.map((event) => {
2426
return {
2527
date: event.start.dateTime,
2628
title: event.summary,
2729
calendarLink: event.htmlLink,
2830
pastEventLink: event.location,
2931
}
3032
})
31-
const upcomingEventData = futureEventsReq.map((event) => {
33+
const upcomingEventData = futureEventsReqData.map((event) => {
3234
return {
3335
date: event.start.dateTime,
3436
title: event.summary,

src/lib/api/fetchNodes.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { EtherscanNodeResponse, MetricReturnData, TimestampedData } from "@/lib/types"
1+
import type {
2+
EtherscanNodeResponse,
3+
MetricReturnData,
4+
TimestampedData,
5+
} from "@/lib/types"
26

37
import { DAYS_TO_FETCH, ETHERSCAN_API_URL } from "@/lib/constants"
48

@@ -23,13 +27,18 @@ export const fetchNodes = async (): Promise<MetricReturnData> => {
2327

2428
try {
2529
const response = await fetch(href)
26-
if (!response.ok) throw new Error("Failed to fetch Etherscan node data")
30+
if (!response.ok) {
31+
console.log(response.status, response.statusText)
32+
throw new Error("Failed to fetch Etherscan node data")
33+
}
2734

2835
const json: EtherscanNodeResponse = await response.json()
29-
const data: TimestampedData<number>[] = json.result.map(({ UTCDate, TotalNodeCount }) => ({
30-
timestamp: new Date(UTCDate).getTime(),
31-
value: +TotalNodeCount,
32-
})).sort((a, b) => a.timestamp - b.timestamp)
36+
const data: TimestampedData<number>[] = json.result
37+
.map(({ UTCDate, TotalNodeCount }) => ({
38+
timestamp: new Date(UTCDate).getTime(),
39+
value: +TotalNodeCount,
40+
}))
41+
.sort((a, b) => a.timestamp - b.timestamp)
3342
const { value } = data[data.length - 1]
3443

3544
return {
@@ -40,4 +49,4 @@ export const fetchNodes = async (): Promise<MetricReturnData> => {
4049
console.error((error as Error).message)
4150
return { error: (error as Error).message }
4251
}
43-
}
52+
}

src/lib/api/fetchTotalEthStaked.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { EthStoreResponse, MetricReturnData, TimestampedData } from "@/lib/types"
1+
import type {
2+
EthStoreResponse,
3+
MetricReturnData,
4+
TimestampedData,
5+
} from "@/lib/types"
26

37
import { weiToRoundedEther } from "@/lib/utils/weiToRoundedEther"
48

@@ -8,31 +12,52 @@ const MS_PER_DAY = 1000 * 60 * 60 * 24
812
const DAY_DELTA = 5
913

1014
export const fetchTotalEthStaked = async (): Promise<MetricReturnData> => {
11-
const { href: ethstoreLatest } = new URL("api/v1/ethstore/latest", BEACONCHA_IN_URL)
15+
const { href: ethstoreLatest } = new URL(
16+
"api/v1/ethstore/latest",
17+
BEACONCHA_IN_URL
18+
)
1219

1320
try {
1421
// 1- Use initial call to `latest` to fetch current Beacon Chain "day" (for use in secondary fetches)
1522
const ethstoreLatestResponse = await fetch(ethstoreLatest)
16-
if (!ethstoreLatestResponse.ok) throw new Error("Failed to fetch Ethstore latest data")
23+
if (!ethstoreLatestResponse.ok) {
24+
console.log(
25+
ethstoreLatestResponse.status,
26+
ethstoreLatestResponse.statusText
27+
)
28+
throw new Error("Failed to fetch Ethstore latest data")
29+
}
1730

1831
const ethstoreJson: EthStoreResponse = await ethstoreLatestResponse.json()
19-
const { data: { day, effective_balances_sum_wei } } = ethstoreJson
32+
const {
33+
data: { day, effective_balances_sum_wei },
34+
} = ethstoreJson
2035
const valueTotalEth = weiToRoundedEther(effective_balances_sum_wei)
2136

22-
const data: TimestampedData<number>[] = [{ timestamp: new Date().getTime(), value: valueTotalEth }]
37+
const data: TimestampedData<number>[] = [
38+
{ timestamp: new Date().getTime(), value: valueTotalEth },
39+
]
2340

2441
// 2- Perform multiple API calls to fetch data for the last 90 days, `getData` for caching
2542
for (let i = DAY_DELTA; i <= DAYS_TO_FETCH; i += DAY_DELTA) {
2643
const lookupDay = day - i
2744
const timestamp = new Date().getTime() - i * MS_PER_DAY
2845

29-
const { href: ethstoreDay } = new URL(`api/v1/ethstore/${lookupDay}`, BEACONCHA_IN_URL)
46+
const { href: ethstoreDay } = new URL(
47+
`api/v1/ethstore/${lookupDay}`,
48+
BEACONCHA_IN_URL
49+
)
3050

3151
const ethstoreDayResponse = await fetch(ethstoreDay)
32-
if (!ethstoreDayResponse.ok) throw new Error("Failed to fetch Ethstore day data")
52+
if (!ethstoreDayResponse.ok) {
53+
console.log(ethstoreDayResponse.status, ethstoreDayResponse.statusText)
54+
throw new Error("Failed to fetch Ethstore day data")
55+
}
3356

3457
const ethstoreDayJson: EthStoreResponse = await ethstoreDayResponse.json()
35-
const { data: { effective_balances_sum_wei: sumWei } } = ethstoreDayJson
58+
const {
59+
data: { effective_balances_sum_wei: sumWei },
60+
} = ethstoreDayJson
3661
const value = weiToRoundedEther(sumWei)
3762

3863
data.push({ timestamp, value })

src/lib/api/fetchTotalValueLocked.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ export const fetchTotalValueLocked = async (): Promise<MetricReturnData> => {
1111

1212
try {
1313
const response = await fetch(`https://api.llama.fi/charts/Ethereum`)
14-
if (!response.ok) throw new Error("Failed to fetch Defi Llama TVL data")
14+
if (!response.ok) {
15+
console.log(response.status, response.statusText)
16+
throw new Error("Failed to fetch Defi Llama TVL data")
17+
}
1518

1619
const json: DefiLlamaTVLResponse = await response.json()
17-
const data = takeRightWhile(
18-
json,
19-
({ date }) => +date > startTimestamp
20-
).map(({ date, totalLiquidityUSD }) => ({
21-
timestamp: +date * 1000,
22-
value: totalLiquidityUSD,
23-
})).sort((a, b) => a.timestamp - b.timestamp)
20+
const data = takeRightWhile(json, ({ date }) => +date > startTimestamp)
21+
.map(({ date, totalLiquidityUSD }) => ({
22+
timestamp: +date * 1000,
23+
value: totalLiquidityUSD,
24+
}))
25+
.sort((a, b) => a.timestamp - b.timestamp)
2426
const { value } = data[data.length - 1]
2527

2628
return {

src/lib/api/fetchTxCount.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { EtherscanTxCountResponse, MetricReturnData, TimestampedData } from "@/lib/types"
1+
import type {
2+
EtherscanTxCountResponse,
3+
MetricReturnData,
4+
TimestampedData,
5+
} from "@/lib/types"
26

37
import { DAYS_TO_FETCH, ETHERSCAN_API_URL } from "@/lib/constants"
48

@@ -23,13 +27,18 @@ export const fetchTxCount = async (): Promise<MetricReturnData> => {
2327

2428
try {
2529
const response = await fetch(href)
26-
if (!response.ok) throw new Error("Failed to fetch Etherscan tx count data")
30+
if (!response.ok) {
31+
console.log(response.status, response.statusText)
32+
throw new Error("Failed to fetch Etherscan tx count data")
33+
}
2734

2835
const json: EtherscanTxCountResponse = await response.json()
29-
const data: TimestampedData<number>[] = json.result.map(({ unixTimeStamp, transactionCount }) => ({
30-
timestamp: +unixTimeStamp * 1000, // unix milliseconds
31-
value: transactionCount,
32-
})).sort((a, b) => a.timestamp - b.timestamp)
36+
const data: TimestampedData<number>[] = json.result
37+
.map(({ unixTimeStamp, transactionCount }) => ({
38+
timestamp: +unixTimeStamp * 1000, // unix milliseconds
39+
value: transactionCount,
40+
}))
41+
.sort((a, b) => a.timestamp - b.timestamp)
3342
const { value } = data[data.length - 1]
3443

3544
return {
@@ -39,7 +48,7 @@ export const fetchTxCount = async (): Promise<MetricReturnData> => {
3948
} catch (error: unknown) {
4049
console.error((error as Error).message)
4150
return {
42-
error: (error as Error).message
51+
error: (error as Error).message,
4352
}
4453
}
45-
}
54+
}

src/lib/api/ghRepoData.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import axios from 'axios'
1+
import axios from "axios"
22

3-
import { Framework } from '@/lib/interfaces'
3+
import { Framework } from "@/lib/interfaces"
44

55
import EthDiamondBlackImage from "@/public/assets/eth-diamond-black.png"
66
import EpirusImage from "@/public/dev-tools/epirus.png"
@@ -128,12 +128,26 @@ export const ghRepoData = async (githubUrl: string) => {
128128
const split = githubUrl.split("/")
129129
const repoOwner = split[split.length - 2]
130130
const repoName = split[split.length - 1]
131-
const repoData = await axios.get(`https://api.github.com/repos/${repoOwner}/${repoName}`, { headers: { 'Authorization': `Bearer ${process.env.GITHUB_TOKEN_READ_ONLY}` }})
132-
const languageData = await axios.get(`https://api.github.com/repos/${repoOwner}/${repoName}/languages`, { headers: { 'Authorization': `Bearer ${process.env.GITHUB_TOKEN_READ_ONLY}` }})
133-
return {
134-
starCount: repoData.data.stargazers_count,
135-
languages: Object.keys(languageData.data),
131+
const repoData = await axios.get(
132+
`https://api.github.com/repos/${repoOwner}/${repoName}`,
133+
{
134+
headers: {
135+
Authorization: `Bearer ${process.env.GITHUB_TOKEN_READ_ONLY}`,
136+
},
136137
}
138+
)
139+
const languageData = await axios.get(
140+
`https://api.github.com/repos/${repoOwner}/${repoName}/languages`,
141+
{
142+
headers: {
143+
Authorization: `Bearer ${process.env.GITHUB_TOKEN_READ_ONLY}`,
144+
},
145+
}
146+
)
147+
return {
148+
starCount: repoData.data.stargazers_count,
149+
languages: Object.keys(languageData.data),
150+
}
137151
}
138152

139153
export const getLocalEnvironmentFrameworkData = async () => {
@@ -148,4 +162,4 @@ export const getLocalEnvironmentFrameworkData = async () => {
148162
})
149163
)
150164
return frameworksListData
151-
}
165+
}

src/lib/api/stablecoinsData.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ export async function fetchEthereumEcosystemData() {
66
try {
77
const res = await fetch(url)
88

9-
return res.json()
9+
if (!res.ok) {
10+
console.log(res.status, res.statusText)
11+
throw new Error("Failed to fetch Ethereum ecosystem data")
12+
}
13+
14+
return await res.json()
1015
} catch (error) {
1116
// In production mode, throw an error to stop the build in case this fetch fails
1217
console.error(error)
@@ -22,7 +27,12 @@ export async function fetchEthereumStablecoinsData() {
2227
try {
2328
const res = await fetch(url)
2429

25-
return res.json()
30+
if (!res.ok) {
31+
console.log(res.status, res.statusText)
32+
throw new Error("Failed to fetch Ethereum stablecoins data")
33+
}
34+
35+
return await res.json()
2636
} catch (error) {
2737
// In production mode, throw an error to stop the build in case this fetch fails
2838
console.error(error)

0 commit comments

Comments
 (0)