-
-
Notifications
You must be signed in to change notification settings - Fork 63
fix: add try-catch for querySelector and log fallbackCopy errors (#132) #199
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,10 @@ const ANALYTICS_SCHEMA_VERSION = "1.0"; | |
| class SocialShareButton { | ||
| constructor(options = {}) { | ||
| // Resolve container element early to prevent duplicate instances | ||
| const containerEl = SocialShareButton._resolveContainer(options.container); | ||
| const containerEl = SocialShareButton._resolveContainer( | ||
| options.container, | ||
| options.debug | ||
| ); | ||
|
|
||
| if (containerEl && containerEl._socialShareButtonInstance) { | ||
| return containerEl._socialShareButtonInstance; | ||
|
|
@@ -533,7 +536,8 @@ class SocialShareButton { | |
| copyBtn.classList.remove("copied"); | ||
| this.feedbackTimeout = null; | ||
| }, 2000); | ||
| } catch (_err) { | ||
| } catch (error) { | ||
| this._debugWarn("fallbackCopy failed", error); | ||
| copyBtn.textContent = "Failed"; | ||
|
|
||
| // Clear any existing feedback timeout | ||
|
|
@@ -732,10 +736,23 @@ class SocialShareButton { | |
| // --------------------------------------------------------------------------- | ||
|
|
||
| // Resolves a raw container value (string or Element) to a DOM Element, or null if absent/SSR. | ||
| static _resolveContainer(raw) { | ||
|
|
||
| static _resolveContainer(raw, debug = false) { | ||
| if (!raw) return null; | ||
| if (typeof document === "undefined") return null; | ||
| return typeof raw === "string" ? document.querySelector(raw) : raw; | ||
| if (typeof raw !== "string") return raw; | ||
|
|
||
| try { | ||
| return document.querySelector(raw); | ||
| } catch (error) { | ||
| if (debug) { | ||
| // Invalid selectors are only logged when debug mode is enabled. | ||
| // eslint-disable-next-line no-console | ||
| console.warn("[SocialShareButton] Invalid container selector:", raw, error); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
Comment on lines
+739
to
+755
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Route selector diagnostics through the shared warning helper. Lines 745-746 introduce another inline Based on learnings, debugging logs in 🤖 Prompt for AI AgentsSource: Learnings |
||
| } | ||
|
|
||
| // Returns the cached host container element, or null. | ||
|
|
@@ -744,16 +761,16 @@ class SocialShareButton { | |
| } | ||
|
|
||
| /** | ||
| * Logs analytics warnings only when debug mode is enabled. | ||
| * @param {string} message - Description of the failed analytics path. | ||
| * @param {Error} err - The caught error instance. | ||
| */ | ||
| _debugWarn(message, err) { | ||
| // _debugWarn: emit analytics warnings only in debug mode for visibility. | ||
| if (!this.options.debug) return; | ||
| // eslint-disable-next-line no-console | ||
| console.warn("[SocialShareButton Analytics]", message, err); | ||
| } | ||
| * Logs debug warnings only when debug mode is enabled. | ||
| * @param {string} message - Warning message. | ||
| * @param {Error} err - The caught error instance. | ||
| */ | ||
| _debugWarn(message, err) { | ||
| if (!this.options.debug) return; | ||
|
|
||
| // eslint-disable-next-line no-console | ||
| console.warn("[SocialShareButton]", message, err); | ||
| } | ||
|
|
||
| /** | ||
| * Emits an analytics event through all configured delivery paths. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.