Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert synthentic status codes to errors #285

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,23 @@ export const getRetrievalResult = measurement => {

if (measurement.timeout) return 'TIMEOUT'
if (measurement.car_too_large) return 'CAR_TOO_LARGE'

if (measurement.status_code >= 700 && measurement.status_code < 800) {
return 'UNSUPPORTED_MULTIADDR_FORMAT'
}

switch (measurement.status_code) {
case 502: return 'BAD_GATEWAY'
case 504: return 'GATEWAY_TIMEOUT'
case 600: return 'UNKNOWN_FETCH_ERROR'
case 801: return 'HOSTNAME_DNS_ERROR'
case 802: return 'CONNECTION_REFUSED'
case 901: return 'UNSUPPORTED_CID_HASH_ALGO'
case 902: return 'CONTENT_VERIFICATION_FAILED'
case 903: return 'UNEXPECTED_CAR_BLOCK'
case 904: return 'CANNOT_PARSE_CAR_FILE'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to move these status code to a shared node module eventually

}

if (measurement.status_code >= 300) return `ERROR_${measurement.status_code}`

const ok = measurement.status_code >= 200 && typeof measurement.end_at === 'string'
Expand Down
8 changes: 8 additions & 0 deletions lib/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export type RetrievalResult =
| 'CAR_TOO_LARGE'
| 'BAD_GATEWAY'
| 'GATEWAY_TIMEOUT'
| 'UNKNOWN_FETCH_ERROR'
| 'UNSUPPORTED_MULTIADDR_FORMAT'
| 'HOSTNAME_DNS_ERROR'
| 'CONNECTION_REFUSED'
| 'UNSUPPORTED_CID_HASH_ALGO'
| 'CONTENT_VERIFICATION_FAILED'
| 'UNEXPECTED_CAR_BLOCK'
| 'CANNOT_PARSE_CAR_FILE'
| 'IPNI_NOT_QUERIED'
| `IPNI_${string}`
| `ERROR_${number}`
Expand Down
66 changes: 66 additions & 0 deletions test/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,70 @@ describe('getRetrievalResult', () => {
})
assert.strictEqual(result, 'IPNI_ERROR_FETCH')
})

for (const code of [701, 702, 703, 704]) {
it(`UNSUPPORTED_MULTIADDR_FORMAT - ${code}`, () => {
const result = getRetrievalResult({
...SUCCESSFUL_RETRIEVAL,
status_code: code
})
assert.strictEqual(result, 'UNSUPPORTED_MULTIADDR_FORMAT')
})
}

it('UNKNOWN_FETCH_ERROR - 600', () => {
const result = getRetrievalResult({
...SUCCESSFUL_RETRIEVAL,
status_code: 600
})
assert.strictEqual(result, 'UNKNOWN_FETCH_ERROR')
})

it('HOSTNAME_DNS_ERROR - 801', () => {
const result = getRetrievalResult({
...SUCCESSFUL_RETRIEVAL,
status_code: 801
})
assert.strictEqual(result, 'HOSTNAME_DNS_ERROR')
})

it('HOSTNAME_DNS_ERROR - 802', () => {
const result = getRetrievalResult({
...SUCCESSFUL_RETRIEVAL,
status_code: 802
})
assert.strictEqual(result, 'CONNECTION_REFUSED')
})

it('UNSUPPORTED_CID_HASH_ALGO - 901', () => {
const result = getRetrievalResult({
...SUCCESSFUL_RETRIEVAL,
status_code: 901
})
assert.strictEqual(result, 'UNSUPPORTED_CID_HASH_ALGO')
})

it('CONTENT_VERIFICATION_FAILED - 902', () => {
const result = getRetrievalResult({
...SUCCESSFUL_RETRIEVAL,
status_code: 902
})
assert.strictEqual(result, 'CONTENT_VERIFICATION_FAILED')
})

it('UNEXPECTED_CAR_BLOCK - 903', () => {
const result = getRetrievalResult({
...SUCCESSFUL_RETRIEVAL,
status_code: 903
})
assert.strictEqual(result, 'UNEXPECTED_CAR_BLOCK')
})

it('CANNOT_PARSE_CAR_FILE - 904', () => {
const result = getRetrievalResult({
...SUCCESSFUL_RETRIEVAL,
status_code: 904
})
assert.strictEqual(result, 'CANNOT_PARSE_CAR_FILE')
})
})