From ba364270148b7e4dbfa20378669352096553f416 Mon Sep 17 00:00:00 2001 From: Daniel Lorigan Date: Fri, 10 Jan 2025 11:51:35 -0800 Subject: [PATCH] Add error type wrapper and missing error cause --- src/lib/components/app/HealthLink.svelte | 5 +++-- src/lib/utils/util.ts | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lib/components/app/HealthLink.svelte b/src/lib/components/app/HealthLink.svelte index 2775fc77..d2651c2a 100644 --- a/src/lib/components/app/HealthLink.svelte +++ b/src/lib/components/app/HealthLink.svelte @@ -26,6 +26,7 @@ import { goto } from '$app/navigation'; import type { Writable } from 'svelte/store'; import type { SHLAdminParams, SHLClient } from '$lib/utils/managementClient'; + import { ensureError } from '$lib/utils/util'; export let shl: SHLAdminParams; let shlControlled: SHLAdminParams; @@ -63,7 +64,7 @@ try { linkIsActive = await shlClient.isActive(shl.id); } catch (e) { - console.error(e); + console.error(ensureError(e).message); linkNotFound = true; } }); @@ -75,7 +76,7 @@ const qrCode = await new Promise((resolve, reject) => { const img = new Image(); img.onload = () => resolve(img); - img.onerror = (err) => reject(new Error('Failed to load image from data URI.')); + img.onerror = (err) => reject(new Error('Failed to load image from data URI.', { cause: err })); img.src = qrCodeURI; }) as HTMLImageElement; // get the header and footer images diff --git a/src/lib/utils/util.ts b/src/lib/utils/util.ts index c5f40623..89663efe 100644 --- a/src/lib/utils/util.ts +++ b/src/lib/utils/util.ts @@ -6,6 +6,17 @@ import type { Bundle, BundleEntry, Resource } from 'fhir/r4'; export const base64url = jose.base64url; +export function ensureError(value: unknown): Error { + if (value instanceof Error) return value; + let stringifiedValue = "[Unable to stringify the thrown value]"; + try { + stringifiedValue = JSON.stringify(value); + } catch (e) {} + + const error = new Error(`This value was thrown, but it was not an Error object: ${stringifiedValue}`); + return error +} + export function randomStringWithEntropy(entropy = 32): string { const b = new Uint8Array(entropy); crypto.getRandomValues(b);