From 14ee946971e8c1dface939ea315d0c0a1460f8d0 Mon Sep 17 00:00:00 2001 From: Valentyne Stigloher Date: Sun, 10 Mar 2024 17:44:08 +0100 Subject: [PATCH] (errorhandling) ignore loading errors for ad and gtm scripts most of the time these are CORS errors which need separate investigation but as these errors are quite common, they keep coming in sentry --- layouts/default.vue | 19 +++++++++++++++++-- plugins/globals.ts | 3 ++- src/errors.ts | 6 ++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 src/errors.ts diff --git a/layouts/default.vue b/layouts/default.vue index 0dab324b3..2caccbabe 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -40,6 +40,7 @@ import dark from '../plugins/dark.ts'; import sorter from 'avris-sorter'; import { sleep } from '../src/helpers.ts'; import md5 from 'js-md5'; +import { LoadScriptError } from '../src/errors.ts'; import type { default as dialogueBox, DialogueMessage } from '../components/DialogueBox.vue'; import type { Color } from '../src/bootstrap.ts'; @@ -217,14 +218,28 @@ export default dark.extend({ return; } - await this.$loadScript('publift', 'https://cdn.fuseplatform.net/publift/tags/2/3329/fuse.js'); + try { + await this.$loadScript('publift', 'https://cdn.fuseplatform.net/publift/tags/2/3329/fuse.js'); + } catch (error) { + if (error instanceof LoadScriptError) { + return; + } + throw error; + } }, async loadGTM(): Promise { if (!this.adsEnabled) { return; } - await this.$loadScript('gtm', 'https://www.googletagmanager.com/gtag/js?id=G-TDJEP12Q3M'); + try { + await this.$loadScript('gtm', 'https://www.googletagmanager.com/gtag/js?id=G-TDJEP12Q3M'); + } catch (error) { + if (error instanceof LoadScriptError) { + return; + } + throw error; + } window.dataLayer = window.dataLayer || []; function gtag(..._args: unknown[]): void { diff --git a/plugins/globals.ts b/plugins/globals.ts index 29e47c410..4704e62e9 100644 --- a/plugins/globals.ts +++ b/plugins/globals.ts @@ -11,6 +11,7 @@ import type { RootState } from '../store/index.ts'; import translations from '../data/translations.suml'; import baseTranslations from '../locale/_base/translations.suml'; +import { LoadScriptError } from '../src/errors.ts'; declare global { interface Window { @@ -96,7 +97,7 @@ export default ({ app, store }: { app: Vue, store: Store }): void => script.crossOrigin = 'true'; script.addEventListener('load', () => resolve()); script.addEventListener('error', (event) => { - reject(new Error(`failed to load ${name} (${src}): ${typeof event === 'string' ? event : event.type}`)); + reject(new LoadScriptError(name, src, typeof event === 'string' ? event : event.type)); }); document.body.appendChild(script); }); diff --git a/src/errors.ts b/src/errors.ts new file mode 100644 index 000000000..67bde28f7 --- /dev/null +++ b/src/errors.ts @@ -0,0 +1,6 @@ +export class LoadScriptError extends Error { + constructor(scriptName: string, src: string, type: string) { + super(`failed to load ${scriptName} (${src}): ${type}`); + this.name = this.constructor.name; + } +}