Skip to content

Commit

Permalink
feat: check status code and error message when handling invalid liste…
Browse files Browse the repository at this point in the history
…nerId
  • Loading branch information
gpuente committed Jul 8, 2024
1 parent febbe2d commit 86982ff
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/hooks/useClientErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ export type ClientErrorHandler = {
driveId: string,
trigger: Trigger,
status: number,
errorMessage: string,
) => Promise<void>;
};

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<string[]>([]);
const [pullResponderTriggerMap, setPullResponderTriggerMap] = useState<
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 86982ff

Please sign in to comment.