Skip to content

Commit c9a526c

Browse files
author
y86993
authored
Add Error Handling
Adding some error handling.
1 parent 285cee4 commit c9a526c

File tree

1 file changed

+69
-44
lines changed

1 file changed

+69
-44
lines changed

src/index.ts

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const tryGet = async (key: string, db: Level<string, string>) => {
2828
}
2929
}
3030

31+
interface IPFSResponse {
32+
hash: string
33+
}
34+
3135
async function main() {
3236
const db = new Level('./db')
3337
console.info()
@@ -62,57 +66,75 @@ async function main() {
6266
console.info(` got cache: ipfs://${cachedMultihash}`)
6367
continue
6468
} else {
65-
const imageBuffer = await (
66-
await fetch(`${process.env.IPFS_GATEWAY}${token.logoURI}`)
67-
).buffer()
69+
const imageUrl = `${process.env.IPFS_GATEWAY}${token.logoURI}`
70+
console.info(`Fetching image from URL: ${imageUrl}`)
71+
const imageBuffer = await (await fetch(imageUrl)).buffer()
72+
73+
try {
74+
const imageSharp = sharp(imageBuffer)
75+
const metadata = await imageSharp.metadata()
76+
77+
console.info(`Image metadata:`, metadata)
78+
79+
if (!metadata.format) {
80+
throw new Error('Unsupported image format')
81+
}
82+
83+
const resizedImageBuffer = await imageSharp.resize(64, 64).png().toBuffer()
6884

69-
const imageSharp = await sharp(imageBuffer).resize(64, 64).png()
70-
const resizedImageBuffer = await imageSharp.toBuffer()
85+
console.info(` Pinning shrunk image to ${process.env.IPFS_GATEWAY}`)
86+
let ipfsResponse: IPFSResponse[] | null = null
7187

72-
console.info(` Pinning shrunk image to ${process.env.IPFS_GATEWAY}`)
73-
let ipfsResponse
88+
for (let attempt = 1; attempt <= 10; attempt++) {
89+
try {
90+
ipfsResponse = await ipfsPublish(
91+
`${token.symbol}.png`,
92+
resizedImageBuffer,
93+
)
94+
console.info(` Done.`)
95+
break
96+
} catch (err) {
97+
console.warn(
98+
` Failed to upload ${token.symbol} to gateway IPFS.`,
99+
err,
100+
)
101+
if (attempt === 5) {
102+
console.error(
103+
` Could not upload ${token.symbol} image to gateway IPFS after 5 attempts.`,
104+
)
105+
} else {
106+
console.warn(` Retrying ${attempt + 1} of ${5}`)
107+
}
108+
}
109+
}
74110

75-
for (let attempt = 1; attempt <= 10; attempt++)
76-
try {
77-
ipfsResponse = await ipfsPublish(
78-
`${token.symbol}.png`,
79-
resizedImageBuffer,
111+
if (!ipfsResponse) {
112+
console.error()
113+
throw new Error(
114+
`Failed to upload ${token.symbol} image to ipfs gateway. Halting`,
80115
)
81-
console.info(` Done.`)
82-
break
83-
} catch (err) {
84-
console.warn(
85-
` Failed to upload ${token.symbol} to gateway IPFS.`,
86-
err,
116+
}
117+
118+
const multihash = ipfsResponse[0].hash
119+
120+
if (ipfsResponse) {
121+
// Was successfully pinned to IPFS, no point in resubmitting.
122+
console.log(` Caching ${multihash}`)
123+
await db.put(
124+
cacheName(token.logoURI as string, token.symbol),
125+
multihash,
87126
)
88-
if (attempt === 5)
89-
console.error(
90-
` Could not upload ${token.symbol} image to gateway IPFS after 5 attempts.`,
91-
)
92-
else console.warn(` Retrying ${attempt + 1} of ${5}`)
93127
}
94128

95-
if (!ipfsResponse) {
96-
console.error()
97-
throw new Error(
98-
`Failed to upload ${token.symbol} image to ipfs gateway. Halting`,
99-
)
100-
}
101-
const multihash = ipfsResponse[0].hash
102-
103-
if (ipfsResponse) {
104-
// Was successfully pinned to IPFS, no point in resubmitting.
105-
console.log(` Caching ${multihash}`)
106-
await db.put(
107-
cacheName(token.logoURI as string, token.symbol),
108-
multihash,
109-
)
129+
tokensWithLogo.push({
130+
...token,
131+
logoURI: `ipfs://${multihash}`,
132+
})
133+
} catch (err) {
134+
console.error(`Failed to process image for token ${token.symbol}:`, err)
135+
console.error(`Token details:`, token)
136+
throw err // stop execution on error
110137
}
111-
112-
tokensWithLogo.push({
113-
...token,
114-
logoURI: `ipfs://${multihash}`,
115-
})
116138
}
117139
}
118140

@@ -127,4 +149,7 @@ async function main() {
127149
)
128150
}
129151

130-
main()
152+
main().catch((err) => {
153+
console.error('Unhandled error:', err)
154+
process.exit(1) // Added to ensure the script exits with an error code
155+
})

0 commit comments

Comments
 (0)