Skip to content

Commit

Permalink
fixup! improve error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <oss@bajtos.net>
  • Loading branch information
bajtos committed Mar 7, 2024
1 parent 2d6b8fc commit a4a26fc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
9 changes: 7 additions & 2 deletions lib/miner-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import { RPC_REQUEST } from './constants.js'
* @returns {Promise<string>} Miner's PeerId, e.g. `12D3KooWMsPmAA65yHAHgbxgh7CPkEctJHZMeM3rAvoW8CZKxtpG`
*/
export async function lookupMinerPeerId (minerId) {
const res = await rpc('Filecoin.StateMinerInfo', minerId, null)
return res.PeerId
try {
const res = await rpc('Filecoin.StateMinerInfo', minerId, null)
return res.PeerId
} catch (err) {
err.message = `Cannot lookup miner ${minerId}: ${err.message}`
throw err
}
}

/**
Expand Down
36 changes: 29 additions & 7 deletions lib/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,23 @@ export default class Spark {
stats.providerId = peerId
} catch (err) {
console.error(err)
this.#activity.onError()
// There are three common error cases:
// 1. We are offline
// 2. The JSON RPC provider is down
// 3. JSON RPC errors like when Miner ID is not a known actor
// There isn't much we can do in the first two cases. We can notify the user that we are not
// performing any jobs and wait until the problem is resolved.
// The third case should not happen unless we made a mistake, so we want to learn about it
if (err.name === 'FilecoinRpcError') {
// TODO: report the error to Sentry
console.error('The error printed above was not expected, please report it on GitHub:')
console.error('https://github.com/filecoin-station/spark/issues/new')
} else {
this.#activity.onError()
}
err.reported = true
// Abort the check, no measurement should be recorded
throw err
}

console.log(`Querying IPNI to find retrieval providers for ${retrieval.cid}`)
Expand Down Expand Up @@ -200,12 +216,7 @@ export default class Spark {
await this.nextRetrieval()
this.#activity.onHealthy()
} catch (err) {
if (err.statusCode === 400 && err.serverMessage === 'OUTDATED CLIENT') {
this.#activity.onOutdatedClient()
} else {
this.#activity.onError()
}
console.error(err)
this.handleRunError(err)
}
const duration = Date.now() - started
const baseDelay = APPROX_ROUND_LENGTH_IN_MS / this.#maxTasksPerNode
Expand All @@ -216,6 +227,17 @@ export default class Spark {
}
}
}

handleRunError (err) {
if (err.reported) return

if (err.statusCode === 400 && err.serverMessage === 'OUTDATED CLIENT') {
this.#activity.onOutdatedClient()
} else {
this.#activity.onError()
}
console.error(err)
}
}

async function assertOkResponse (res, errorMsg) {
Expand Down
2 changes: 1 addition & 1 deletion test/miner-lookup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ test('lookup peer id of a miner that does not exist', async () => {
`Expected "lookupMinerPeerId()" to fail, but it resolved with "${result}" instead.`
)
} catch (err) {
assertMatch(err.toString(), /actor code is not miner/)
assertMatch(err.toString(), /\bf010\b.*\bactor code is not miner/)
}
})

0 comments on commit a4a26fc

Please sign in to comment.