Skip to content

Commit

Permalink
fix: error not being set if set failed with no retry
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeistrich committed Sep 25, 2024
1 parent c3f0ed2 commit 74c71e2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/sync/syncObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ async function doChangeRemote(changeInfo: PreppedChangeRemote | undefined) {
| undefined = undefined;

let errorHandled = false;
const onError = (error: Error, retryParams: OnErrorRetryParams) => {
const onError = (error: Error, retryParams?: OnErrorRetryParams) => {
state$.error.set(error);
if (!errorHandled) {
syncOptions.onError?.(error, {
Expand Down Expand Up @@ -681,8 +681,11 @@ async function doChangeRemote(changeInfo: PreppedChangeRemote | undefined) {
let didError = false;

if (isPromise(savedPromise)) {
await savedPromise.catch(() => {
await savedPromise.catch((error) => {
didError = true;
if (!syncOptions.retry) {
onError(error);
}
});
}

Expand Down
67 changes: 67 additions & 0 deletions tests/crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2573,4 +2573,71 @@ describe('Error is set', () => {

expect(syncState(obs$).error.get()).toEqual(new Error('test'));
});
test('error is set if create fails', async () => {
const obs$ = observable(
syncedCrud({
list: () => promiseTimeout(0, [ItemBasicValue()]),
as: 'object',
create: async () => {
throw new Error('test');
},
}),
);

expect(obs$.get()).toEqual(undefined);

await promiseTimeout(1);

obs$.id2.set({ id: '2', test: 'hi' });

await promiseTimeout(1);

expect(syncState(obs$).error.get()).toEqual(new Error('test'));
});
test('error is set if create fails', async () => {
const obs$ = observable(
syncedCrud({
list: () => promiseTimeout(0, [ItemBasicValue()]),
as: 'object',
update: async () => {
throw new Error('test');
},
}),
);

expect(obs$.get()).toEqual(undefined);

await promiseTimeout(1);

obs$.id1.test.set('hello');

await promiseTimeout(1);

expect(syncState(obs$).error.get()).toEqual(new Error('test'));
});
test('onError is called if create fails', async () => {
let errorAtOnError: Error | undefined = undefined;
const obs$ = observable(
syncedCrud({
list: () => promiseTimeout(0, [ItemBasicValue()]),
as: 'object',
create: async () => {
throw new Error('test');
},
onError: (error) => {
errorAtOnError = error;
},
}),
);

expect(obs$.get()).toEqual(undefined);

await promiseTimeout(1);

obs$.id2.set({ id: '2', test: 'hi' });

await promiseTimeout(1);

expect(errorAtOnError).toEqual(new Error('test'));
});
});

0 comments on commit 74c71e2

Please sign in to comment.