From 86982ffa16aea9667f6083263ecea8c8ac49ad43 Mon Sep 17 00:00:00 2001 From: Guillermo Puente Date: Mon, 8 Jul 2024 10:45:07 -0400 Subject: [PATCH] feat: check status code and error message when handling invalid listenerId --- src/hooks/useClientErrorHandler.ts | 46 +++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/hooks/useClientErrorHandler.ts b/src/hooks/useClientErrorHandler.ts index 303528c9..2f49ad01 100644 --- a/src/hooks/useClientErrorHandler.ts +++ b/src/hooks/useClientErrorHandler.ts @@ -11,11 +11,20 @@ export type ClientErrorHandler = { driveId: string, trigger: Trigger, status: number, + errorMessage: string, ) => Promise; }; const DELAY_LIMIT = 100000; +const isListenerIdNotFound = (errorMessage: string, listenerId?: string) => { + if (!listenerId) return false; + + return errorMessage + .toLocaleLowerCase() + .includes(`transmitter ${listenerId} not found`); +}; + export const useClientErrorHandler = (): ClientErrorHandler => { const [handlingInProgress, setHandlingInProgress] = useState([]); const [pullResponderTriggerMap, setPullResponderTriggerMap] = useState< @@ -84,22 +93,31 @@ export const useClientErrorHandler = (): ClientErrorHandler => { ); const strandsErrorHandler: ClientErrorHandler['strandsErrorHandler'] = - async (driveId, trigger, status) => { + async (driveId, trigger, status, errorMessage) => { switch (status) { case 400: { - const handlerCode = `strands:${driveId}:${status}`; - - if (handlingInProgress.includes(handlerCode)) return; - if (!trigger.data) return; - - const delay = - pullResponderRegisterDelay.current.get(handlerCode) || - 0; - - setTimeout( - () => handleStrands400(driveId, trigger, handlerCode), - delay, - ); + if ( + isListenerIdNotFound( + errorMessage, + trigger.data?.listenerId, + ) + ) { + const handlerCode = `strands:${driveId}:${status}`; + + if (handlingInProgress.includes(handlerCode)) return; + if (!trigger.data) return; + + const delay = + pullResponderRegisterDelay.current.get( + handlerCode, + ) || 0; + + setTimeout( + () => + handleStrands400(driveId, trigger, handlerCode), + delay, + ); + } break; }