Skip to content

Commit

Permalink
Merge pull request #438 from powerhouse-inc/fix-starnds-query-error-h…
Browse files Browse the repository at this point in the history
…andler-status

feat: handle invalid listener id with status error 400 instead of 404
  • Loading branch information
gpuente authored Jul 8, 2024
2 parents e4e72de + 86982ff commit 84c332a
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 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 All @@ -26,7 +35,7 @@ export const useClientErrorHandler = (): ClientErrorHandler => {

const pullResponderRegisterDelay = useRef<Map<string, number>>(new Map());

const handleStrands404 = useCallback(
const handleStrands400 = useCallback(
async (driveId: string, trigger: Trigger, handlerCode: string) => {
setHandlingInProgress(state => [...state, handlerCode]);

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 404: {
const handlerCode = `strands:${driveId}:${status}`;

if (handlingInProgress.includes(handlerCode)) return;
if (!trigger.data) return;

const delay =
pullResponderRegisterDelay.current.get(handlerCode) ||
0;

setTimeout(
() => handleStrands404(driveId, trigger, handlerCode),
delay,
);
case 400: {
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 84c332a

Please sign in to comment.