Skip to content

Commit

Permalink
fix handle piece indexer not found response
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Jan 31, 2025
1 parent 58fe8af commit 88c6d88
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions backend/lib/piece-indexer-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const PieceIndexerResponse = Type.Object({
samples: Type.Array(Type.String())
})

const PieceIndexerErrorResponse = Type.Object({
error: Type.String()
})

/**
* @param {string} providerId
* @param {string} pieceCid
Expand All @@ -20,6 +24,14 @@ export const getDealPayloadCid = async (providerId, pieceCid) => {
headers: { 'content-type': 'application/json' }
}), { retries: 5 })
const json = await response.json()

try {
const parsedPixResponse = Value.Parse(PieceIndexerErrorResponse, json)
if (parsedPixResponse.error === 'PROVIDER_OR_PIECE_NOT_FOUND') {
return null
}
} catch {}

try {
const parsedPixResponse = Value.Parse(PieceIndexerResponse, json)
return parsedPixResponse.samples.length === 0
Expand Down
12 changes: 12 additions & 0 deletions backend/test/piece-indexer-service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { it, describe } from 'node:test'
import { getDealPayloadCid } from '../lib/piece-indexer-service.js'
import assert from 'node:assert/strict'

describe('piece-indexer-service', () => {
describe('integration', () => {
it('ignores missing deal payload cids', async () => {
const sample = await getDealPayloadCid('f0notfound', 'bafynotfound')
assert.strictEqual(sample, null)
})
})
})

0 comments on commit 88c6d88

Please sign in to comment.