From 17bf9b8e0b4c5744b125d4c926478ad6f80564ff Mon Sep 17 00:00:00 2001 From: Jackie Quach Date: Thu, 27 Jun 2024 10:38:57 -0400 Subject: [PATCH] add error state for PDFReader --- src/PdfReader/index.tsx | 15 ++++++++++++++- src/PdfReader/reducer.ts | 8 ++++++++ src/PdfReader/types.ts | 10 +++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/PdfReader/index.tsx b/src/PdfReader/index.tsx index 2ea8b84b..105406ad 100644 --- a/src/PdfReader/index.tsx +++ b/src/PdfReader/index.tsx @@ -244,6 +244,8 @@ export default function usePdfReader(args: ReaderArguments): ReaderReturn { }; } + if (state.state === 'ERROR') throw state.loadError; + // if (isFetching) { // // The Reader is fetching a PDF resource // return { @@ -283,6 +285,13 @@ export default function usePdfReader(args: ReaderArguments): ReaderReturn { }); }; + const onDocumentLoadError = (error: Error) => { + dispatch({ + type: 'PDF_LOAD_ERROR', + error: error, + }); + }; + function onRenderSuccess(page: PageProps) { if (!page.height || !page.width) throw new Error( @@ -331,7 +340,11 @@ export default function usePdfReader(args: ReaderArguments): ReaderReturn { } `} - + {isParsed && state.numPages && ( <> {state.settings.isScrolling && diff --git a/src/PdfReader/reducer.ts b/src/PdfReader/reducer.ts index d72be961..83ad5022 100644 --- a/src/PdfReader/reducer.ts +++ b/src/PdfReader/reducer.ts @@ -171,6 +171,14 @@ export function makePdfReducer( state.pageNumber === -1 ? action.numPages : state.pageNumber, }; + case 'PDF_LOAD_ERROR': + return { + ...state, + state: 'ERROR', + loadError: action.error, + settings: DEFAULT_SETTINGS, + }; + case 'SET_SCROLL': if (state.state !== 'ACTIVE') { return handleInvalidTransition(state, action); diff --git a/src/PdfReader/types.ts b/src/PdfReader/types.ts index 59a396e7..8023ba26 100644 --- a/src/PdfReader/types.ts +++ b/src/PdfReader/types.ts @@ -22,7 +22,14 @@ export type InactiveState = ReaderState & export type ActiveState = ReaderState & InternalState & { state: 'ACTIVE'; settings: ReaderSettings }; -export type PdfState = InactiveState | ActiveState; +export type ErrorState = ReaderState & + InternalState & { + state: 'ERROR'; + loadError: Error; + settings: ReaderSettings; + }; + +export type PdfState = InactiveState | ActiveState | ErrorState; export type PdfReaderAction = | { @@ -34,6 +41,7 @@ export type PdfReaderAction = | { type: 'GO_TO_HREF'; href: string } | { type: 'RESOURCE_FETCH_SUCCESS'; resource: { data: Uint8Array } } | { type: 'PDF_PARSED'; numPages: number } + | { type: 'PDF_LOAD_ERROR'; error: Error } | { type: 'SET_SCALE'; scale: number } | { type: 'SET_SCROLL'; isScrolling: boolean } | { type: 'PAGE_LOAD_SUCCESS'; height: number; width: number }