Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print cloud errors in console if present. #334

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions demos/editor-cdn/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</template>

<script setup lang="ts">
import { ref, reactive, computed, effect } from 'vue';
import { ref, reactive, computed } from 'vue';
import useCKEditorCloud from '../../src/useCKEditorCloud.js';
import type { EventInfo, ClassicEditor } from 'https://cdn.ckeditor.com/typings/ckeditor5.d.ts';

Expand All @@ -46,12 +46,6 @@ const cloud = useCKEditorCloud( {
version: '43.0.0'
} );

effect( () => {
if ( cloud.error.value ) {
console.error( cloud.error.value );
}
} );

const TestEditor = computed<typeof ClassicEditor | null>( () => {
if ( !cloud.data.value ) {
return null;
Expand Down
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