diff --git a/src/utils/errorReporting.ts b/src/utils/errorReporting.ts index b4ef74574..71ecf6d2a 100644 --- a/src/utils/errorReporting.ts +++ b/src/utils/errorReporting.ts @@ -18,7 +18,11 @@ export const init = (app: App) => { dsn: VITE_SENTRY_DSN, }); - Sentry.setContext('gpu', getGPUInfo()); + try { + Sentry.setContext('gpu', getGPUInfo()); + } catch (err) { + Sentry.captureException(err); + } }; const setEnabled = (enabled: boolean) => { diff --git a/src/utils/gpuInfo.ts b/src/utils/gpuInfo.ts index cfe59ac22..01053b571 100644 --- a/src/utils/gpuInfo.ts +++ b/src/utils/gpuInfo.ts @@ -1,22 +1,24 @@ -import { Maybe } from '@/src/types'; +function getContextFromOffscreenCanvas() { + if (typeof OffscreenCanvas === 'undefined') return null; + const canvas = new OffscreenCanvas(1, 1); + return canvas.getContext('webgl2') as WebGL2RenderingContext | null; +} + +function getContextFromHTMLCanvas() { + if (typeof document === 'undefined') return null; + const canvas = document.createElement('canvas'); + return canvas.getContext('webgl2') as WebGL2RenderingContext | null; +} /** * Retrieves the GPU renderer and vendor info. * @returns */ export function getGPUInfo() { - let canvas: Maybe = null; - if (typeof OffscreenCanvas !== 'undefined') { - canvas = new OffscreenCanvas(1, 1); - } else if (typeof document !== 'undefined') { - canvas = document.createElement('canvas'); - } else { - throw new Error('Cannot init a canvas'); - } - - const gl = canvas.getContext('webgl2') as WebGL2RenderingContext; + // try offscreencanvas to support usage in webworkers + const gl = getContextFromOffscreenCanvas() ?? getContextFromHTMLCanvas(); if (!gl) { - throw new Error('Cannot get a WebGL2 context'); + throw new Error('Cannot get a webgl2 context'); } const info = {