Skip to content

Commit

Permalink
add more details to keel onError function
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeistrich committed Jun 28, 2024
1 parent 8f8003f commit c6fb295
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/sync-plugins/keel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ export interface SyncedKeelConfiguration
realtimePlugin?: KeelRealtimePlugin;
as?: Exclude<CrudAsOption, 'value'>;
enabled?: boolean;
onError?: (params: APIResult<any>['error']) => void;
onError?: (params: {
type: 'create' | 'update' | 'delete';
params: SyncedSetParams<any>;
input: any;
action: string;
error: APIResult<any>['error'];
}) => void;
}

interface PageInfo {
Expand Down Expand Up @@ -408,6 +414,8 @@ export function syncedKeel<
const handleSetError = async (
error: APIError,
params: SyncedSetParams<TRemote>,
input: TRemote,
fn: Function,
from: 'create' | 'update' | 'delete',
) => {
const { retryNum, cancelRetry, update } = params;
Expand All @@ -434,7 +442,7 @@ export function syncedKeel<
cancelRetry();
}
} else if (error.type === 'bad_request') {
keelConfig.onError?.(error);
keelConfig.onError?.({ error, params, input, type: from, action: fn.name || fn.toString() });

if (retryNum > 4) {
cancelRetry();
Expand All @@ -450,10 +458,11 @@ export function syncedKeel<

const create = createParam
? async (input: TRemote, params: SyncedSetParams<TRemote>) => {
console.log(createParam.toString());
const { data, error } = await createParam(convertObjectToCreate(input));

if (error) {
await handleSetError(error, params, 'create');
await handleSetError(error, params, input, createParam, 'create');
}

return data;
Expand All @@ -462,6 +471,8 @@ export function syncedKeel<

const update = updateParam
? async (input: TRemote, params: SyncedSetParams<TRemote>) => {
console.log(updateParam.toString());

const id = input.id;
const values = convertObjectToCreate(input as unknown as Partial<KeelObjectBase>) as Partial<TRemote> &
Partial<KeelObjectBase>;
Expand All @@ -472,7 +483,7 @@ export function syncedKeel<
const { data, error } = await updateParam({ where: { id }, values });

if (error) {
await handleSetError(error, params, 'update');
await handleSetError(error, params, input, updateParam, 'update');
}

return data;
Expand All @@ -484,7 +495,7 @@ export function syncedKeel<
const { data, error } = await deleteParam({ id: value.id });

if (error) {
await handleSetError(error, params, 'delete');
await handleSetError(error, params, value, deleteParam, 'delete');
}

return data;
Expand Down

0 comments on commit c6fb295

Please sign in to comment.