Skip to content

Commit

Permalink
Refactor retry error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeistrich committed Aug 25, 2024
1 parent 775e86c commit d6b75fb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
9 changes: 6 additions & 3 deletions src/sync-plugins/keel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ export interface SyncedKeelConfiguration
| 'generateId'
> {
client: {
auth: { refresh: () => Promise<boolean>; isAuthenticated: () => Promise<boolean> };
auth: {
refresh: () => Promise<APIResult<boolean>>;
isAuthenticated: () => Promise<APIResult<boolean>>;
};
api: { queries: Record<string, (i: any) => Promise<any>> };
};
realtimePlugin?: KeelRealtimePlugin;
Expand Down Expand Up @@ -454,11 +457,11 @@ export function syncedKeel<
params.cancelRetry = true;
}

throw new Error(error.message);
throw new Error(error.message, { cause: { input } });
} else {
await handleApiError(error);

throw new Error(error.message);
throw new Error(error.message, { cause: { input } });
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/sync/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function runWithRetry<T>(

if (timeout === false) {
state.cancelRetry = true;
reject();
reject(error);
} else {
mapRetryTimeouts.set(state.node, timeout);
timeoutRetry = timeout;
Expand Down
21 changes: 14 additions & 7 deletions src/sync/syncObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ import type {
SyncTransform,
SyncTransformMethod,
Synced,
SyncedErrorParams,
SyncedGetParams,
SyncedOptions,
SyncedSetParams,
SyncedSubscribeParams,
} from './syncTypes';
import { runWithRetry } from './retry';

Expand Down Expand Up @@ -629,7 +631,11 @@ async function doChangeRemote(changeInfo: PreppedChangeRemote | undefined) {

const onError = (error: Error) => {
state$.error.set(error);
syncOptions.onSetError?.(error, setParams as SyncedSetParams<any>);
syncOptions.onSetError?.(error, {
setParams: setParams as SyncedSetParams<any>,
source: 'set',
value$: obs$,
});
};

const setParams: SyncedSetParams<any> = {
Expand Down Expand Up @@ -906,9 +912,9 @@ export function syncObservable<T>(
allSyncStates.set(syncState$, node);
syncStateValue.getPendingChanges = () => localState.pendingChanges;

const onGetError = (error: Error, getParams: SyncedGetParams<T> | undefined, source: 'get' | 'subscribe') => {
const onGetError = (error: Error, params: Omit<SyncedErrorParams, 'value$'>) => {
syncState$.error.set(error);
syncOptions.onGetError?.(error, getParams, source);
syncOptions.onGetError?.(error, { ...params, value$: obs$ });
};

loadLocal(obs$, syncOptions, syncState$, localState);
Expand Down Expand Up @@ -1076,7 +1082,7 @@ export function syncObservable<T>(
const subscribe = syncOptions.subscribe;
isSubscribed = true;
const doSubscribe = () => {
unsubscribe = subscribe({
const subscribeParams: SyncedSubscribeParams<T> = {
node,
value$: obs$,
lastSync,
Expand All @@ -1089,8 +1095,9 @@ export function syncObservable<T>(
});
},
refresh: () => when(syncState$.isLoaded, sync),
onError: (error) => onGetError(error, undefined, 'subscribe'),
});
onError: (error: Error) => onGetError(error, { source: 'subscribe', subscribeParams }),
};
unsubscribe = subscribe(subscribeParams);
};

if (waitFor) {
Expand All @@ -1101,7 +1108,7 @@ export function syncObservable<T>(
}
const existingValue = getNodeValue(node);

const onError = (error: Error) => onGetError(error, getParams as SyncedGetParams<T>, 'get');
const onError = (error: Error) => onGetError(error, { getParams, source: 'get' });

const getParams: SyncedGetParams<T> = {
node,
Expand Down
12 changes: 10 additions & 2 deletions src/sync/syncTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ export interface SyncedSubscribeParams<T = any> extends SyncedGetSetSubscribeBas
onError: (error: Error) => void;
}

export interface SyncedErrorParams {
getParams?: SyncedGetParams<any>;
setParams?: SyncedSetParams<any>;
subscribeParams?: SyncedSubscribeParams<any>;
source: 'get' | 'set' | 'subscribe';
value$: ObservableParam<any>;
}

export interface SyncedOptions<TRemote = any, TLocal = TRemote> extends Omit<LinkedOptions<TRemote>, 'get' | 'set'> {
get?: (params: SyncedGetParams<TRemote>) => Promise<TRemote> | TRemote;
set?: (params: SyncedSetParams<TRemote>) => void | Promise<any>;
Expand All @@ -76,8 +84,8 @@ export interface SyncedOptions<TRemote = any, TLocal = TRemote> extends Omit<Lin
syncMode?: 'auto' | 'manual';
mode?: GetMode;
transform?: SyncTransform<TLocal, TRemote>;
onGetError?: (error: Error, getParams: SyncedGetParams<TRemote> | undefined, source: 'get' | 'subscribe') => void;
onSetError?: (error: Error, setParams: SyncedSetParams<TRemote>) => void;
onGetError?: (error: Error, params: SyncedErrorParams) => void;
onSetError?: (error: Error, params: SyncedErrorParams) => void;
onBeforeGet?: (params: {
value: TRemote;
lastSync: number | undefined;
Expand Down

0 comments on commit d6b75fb

Please sign in to comment.