Skip to content

Commit

Permalink
Print cloud errors in console if present.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mati365 committed Nov 28, 2024
1 parent 1c6dcf0 commit 0f2fc07
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/composables/useAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export const useAsync = <R>(
data.value = result;
}
} catch ( err: any ) {
console.error( err );

if ( !shouldDiscardQuery() ) {
error.value = err;
}
Expand Down
16 changes: 15 additions & 1 deletion tests/composables/useAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* For licensing, see LICENSE.md.
*/

import { it, describe, expect } from 'vitest';
import { it, vi, describe, expect } from 'vitest';
import { ref } from 'vue';
import { flushPromises } from '@vue/test-utils';

Expand Down Expand Up @@ -42,6 +42,20 @@ describe( 'useAsync', () => {
expect( data.value ).toBe( null );
} );

it( 'should print errors in console if the async function throws an error', async () => {
const errorInstance = new Error( 'test' );
const consoleSpy = vi.spyOn( console, 'error' );

useAsync( async () => {
throw errorInstance;
} );

await flushPromises();

expect( consoleSpy ).toHaveBeenCalledWith( errorInstance );
consoleSpy.mockRestore();
} );

it( 'should re-run async function on change ref inside async function', async () => {
const refValue = ref( 0 );
const { data } = useAsync( async () => refValue.value );
Expand Down

0 comments on commit 0f2fc07

Please sign in to comment.