Skip to content

Commit

Permalink
Add error type wrapper and missing error cause
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellrgn committed Jan 10, 2025
1 parent 5140746 commit ba36427
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/lib/components/app/HealthLink.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,7 +64,7 @@
try {
linkIsActive = await shlClient.isActive(shl.id);
} catch (e) {
console.error(e);
console.error(ensureError(e).message);
linkNotFound = true;
}
});
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/lib/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ba36427

Please sign in to comment.