Skip to content

Commit

Permalink
Merge pull request #40 from StauroDEV/fix/logging
Browse files Browse the repository at this point in the history
fix: fix ping logging
  • Loading branch information
talentlessguy authored May 29, 2024
2 parents 9ea5861 + a5436bf commit f9cbcdc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/actions/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,38 @@ export const pingAction = async (

retryCount++
const url = `https://${cid}.ipfs.${endpoint}`
console.log(`${isTTY
logger.text(`${isTTY
? `${colors.bold(`[${retryCount}]`)}: Requesting content at ${url}`
: `[${retryCount}]`}: Requesting content at ${url}`,
)
try {
const response = await fetch(url, { signal: AbortSignal.timeout(timeout), redirect: 'follow' })
if (response.status === 504) {
if (retries > 1) {
console.log(`🔄 Retrying in ${retryInterval / 1000} seconds...`)
logger.text(`🔄 Retrying in ${retryInterval / 1000} seconds...`)
await new Promise(resolve => setTimeout(resolve, retryInterval))
return pingAction({ cid, endpoint, options: { maxRetries: retries - 1, retryInterval } })
}
else {
return console.error(gwOfflineMessage)
return logger.error(gwOfflineMessage)
}
}
else {
return console.log(`Gateway status: ${
return logger.text(`Gateway status: ${
response.status >= 200 && response.status < 400
? logger.info((isTTY ? colors.bold(colors.green(`Online ${response.status}`)) : `Online ${response.status}`))
? (isTTY ? colors.bold(colors.green(`🟢 Online ${response.status}`)) : `🟢 Online ${response.status}`)
: response.status
}`)
}
}
catch (error) {
if (error instanceof DOMException) {
if (retries > 1) {
console.log(`⌛ Timed out. Retrying...`)
logger.info(`⌛ Timed out. Retrying...`)
return pingAction({ cid, endpoint, options: { maxRetries: retries - 1, retryInterval } })
}
else {
return console.error(gwOfflineMessage)
return logger.error(gwOfflineMessage)
}
}
logger.error('Error fetching endpoint:', (error as Error).message)
Expand Down
3 changes: 3 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export const logger = {
if (isTTY) console.log('\n', method === 'GET' ? cyan(method) : green(method), url, responseStatus(status))
else console.log('\n', method, url, status)
},
text(...args: unknown[]) {
console.log(...args)
},
}

export const deployMessage = (provider: string, supports: SupportedMethods) => {
Expand Down

0 comments on commit f9cbcdc

Please sign in to comment.