From 9419375b236d6da1f42a5ffa8a8c76ee855cc5f7 Mon Sep 17 00:00:00 2001 From: Danny Feliz Date: Fri, 29 Mar 2024 01:41:59 -0400 Subject: [PATCH] Always emit the error --- src/App.vue | 29 +++++++++++++++++++++++++++-- src/vue-tweet.vue | 32 ++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/App.vue b/src/App.vue index dfdf27c..7e0f212 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,8 +1,33 @@ + diff --git a/src/vue-tweet.vue b/src/vue-tweet.vue index 524a84d..7a777c2 100644 --- a/src/vue-tweet.vue +++ b/src/vue-tweet.vue @@ -1,6 +1,6 @@ @@ -152,11 +152,11 @@ export default defineComponent({ emits: { "tweet-load-success": (twitterWidgetElement: HTMLDivElement) => !!twitterWidgetElement, - "tweet-load-error": () => true, + "tweet-load-error": (error: Error) => error, }, setup(props, { attrs, emit }) { - const isLoading = ref(true); - const hasError = ref(false); + const isLoading = ref(true); + const hasError = ref(false); const tweetContainerRef = ref(); onMounted(() => { @@ -168,13 +168,9 @@ export default defineComponent({ }); function renderTweet(): void { - console.log(window["twttr"]?.ready) if (!(window["twttr"] && window["twttr"].ready)) { - console.log("should add it") addScript("https://platform.twitter.com/widgets.js", renderTweet); return; - } else { - console.log("already added") } window["twttr"].ready().then(({ widgets }: any) => { @@ -197,7 +193,7 @@ export default defineComponent({ emit("tweet-load-success", twitterWidgetElement); } else { hasError.value = true; - emit("tweet-load-error"); + emit("tweet-load-error", new Error("Failed to load tweet.")); } }) .finally(() => { @@ -209,11 +205,12 @@ export default defineComponent({ function getTweetParams() { let { tweetId, tweetUrl, ...tweetOptions } = props; + let error: Error | null = null; if (tweetId && tweetUrl) { - throw new Error("Cannot provide both tweet-id and tweet-url."); + error = new Error("Cannot provide both tweet-id and tweet-url."); } else if (tweetId) { if (!/^\d+$/.test(tweetId)) { - throw new Error( + error = new Error( "Invalid tweet-id, please provide a valid numerical tweet-id." ); } @@ -222,10 +219,17 @@ export default defineComponent({ if (match) { tweetId = match.groups?.tweetId as string; } else { - throw new Error("Invalid tweet-url."); + error = new Error("Invalid tweet-url."); } } else { - throw new Error("Must provide either tweet-id or tweet-url."); + error = new Error("Must provide either tweet-id or tweet-url."); + } + + if (error) { + hasError.value = true; + isLoading.value = false; + emit("tweet-load-error", error); + throw error; } return {