Skip to content

Commit

Permalink
chore: define custom error types
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Oct 11, 2024
1 parent 39253af commit 930b190
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
18 changes: 18 additions & 0 deletions packages/verified-fetch/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,21 @@ export class InvalidRangeError extends Error {
this.name = 'InvalidRangeError'
}
}

export class NoContentError extends Error {
static name = 'NoContentError'

constructor (message = 'No content found') {
super(message)
this.name = 'NoContentError'
}
}

export class SubdomainNotSupportedError extends Error {
static name = 'SubdomainNotSupportedError'

constructor (message = 'Subdomain not supported') {
super(message)
this.name = 'SubdomainNotSupportedError'
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AbortError, type ComponentLogger } from '@libp2p/interface'
import { CustomProgressEvent } from 'progress-events'
import { NoContentError } from '../errors.js'
import type { VerifiedFetchInit } from '../index.js'

/**
Expand All @@ -12,7 +13,7 @@ export async function getStreamFromAsyncIterable (iterator: AsyncIterable<Uint8A

if (done === true) {
log.error('no content found for path', path)
throw new Error('No content found')
throw new NoContentError()
}

const stream = new ReadableStream({
Expand Down
3 changes: 2 additions & 1 deletion packages/verified-fetch/src/utils/handle-redirects.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type AbortOptions, type ComponentLogger } from '@libp2p/interface'
import { SubdomainNotSupportedError } from '../errors.js'
import { type VerifiedFetchInit, type Resource } from '../index.js'
import { matchURLString } from './parse-url-string.js'
import { movedPermanentlyResponse } from './responses.js'
Expand Down Expand Up @@ -82,7 +83,7 @@ export async function getRedirectResponse ({ resource, options, logger, cid, fet
return movedPermanentlyResponse(resource.toString(), subdomainUrl.href)
} else {
log('subdomain not supported, subdomain failed with status %s %s', subdomainTest.status, subdomainTest.statusText)
throw new Error('subdomain not supported')
throw new SubdomainNotSupportedError('subdomain not supported')
}
} catch (err: any) {
log('subdomain not supported', err)
Expand Down

0 comments on commit 930b190

Please sign in to comment.