Skip to content

Commit

Permalink
feat: added support for custom error
Browse files Browse the repository at this point in the history
  • Loading branch information
mbret committed Aug 22, 2024
1 parent b3092f3 commit cd04504
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
3 changes: 1 addition & 2 deletions packages/streamer/src/ServiceWorkerStreamer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createArchiveLoader } from "./archives/archiveLoader"
import { Streamer } from "./Streamer"
import { removeTrailingSlash } from "./utils/uri"

Expand All @@ -18,7 +17,7 @@ export class ServiceWorkerStreamer extends Streamer {
constructor({
getUriInfo,
...rest
}: Parameters<typeof createArchiveLoader>[0] & {
}: ConstructorParameters<typeof Streamer>[0] & {
getUriInfo: (event: ConflictFreeWebWorkerFetchEvent) =>
| {
baseUrl: string
Expand Down
18 changes: 14 additions & 4 deletions packages/streamer/src/Streamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@ import { generateResourceFromArchive } from "./generators/resources"

export class Streamer {
epubLoader: ReturnType<typeof createArchiveLoader>
onError = (error: unknown) => {
return new Response(String(error), { status: 500 })
}

constructor({
onError,
...rest
}: Parameters<typeof createArchiveLoader>[0] & {
onError?: (error: unknown) => Response
}) {
this.epubLoader = createArchiveLoader(rest)

constructor(params: Parameters<typeof createArchiveLoader>[0]) {
this.epubLoader = createArchiveLoader(params)
this.onError = onError ?? this.onError
}

public fetchManifest({ key, baseUrl }: { key: string; baseUrl?: string }) {
Expand All @@ -39,7 +49,7 @@ export class Streamer {
)
}),
catchError((error) => {
return of(new Response(String(error), { status: 500 }))
return of(this.onError(error))
}),
)

Expand Down Expand Up @@ -67,7 +77,7 @@ export class Streamer {
)
}),
catchError((error) => {
return of(new Response(String(error), { status: 500 }))
return of(this.onError(error))
}),
)

Expand Down

0 comments on commit cd04504

Please sign in to comment.