From 9bc95d4bb11c0a23d8f47d87b7bbc14c01170cdb Mon Sep 17 00:00:00 2001 From: SatsAllDay <128755788+SatsAllDay@users.noreply.github.com> Date: Sun, 24 Mar 2024 14:25:14 -0400 Subject: [PATCH] gracefully handle errors when fetching lnurlp wellknown info (#960) if `fetch` or `req.json` fails, catch those errors and return a default error to the user if the res payload indicates error but doesn't return a `reason`, also return the same default error message to the user --- lib/lnurl.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/lnurl.js b/lib/lnurl.js index a550cb81..11f4c07d 100644 --- a/lib/lnurl.js +++ b/lib/lnurl.js @@ -33,10 +33,19 @@ export async function lnAddrOptions (addr) { // support HTTP and HTTPS during development protocol = process.env.PUBLIC_URL.split('://')[0] } - const req = await fetch(`${protocol}://${domain}/.well-known/lnurlp/${name}`) - const res = await req.json() + const unexpectedErrorMessage = `An unexpected error occurred fetching the Lightning Address metadata for ${addr}. Check the address and try again.` + let res + try { + const req = await fetch(`${protocol}://${domain}/.well-known/lnurlp/${name}`) + res = await req.json() + } catch (err) { + // If `fetch` fails, or if `req.json` fails, catch it here and surface a reasonable error + console.log('Error fetching lnurlp', err) + throw new Error(unexpectedErrorMessage) + } if (res.status === 'ERROR') { - throw new Error(res.reason) + // if the response doesn't adhere to spec by providing a `reason` entry, returns a default error message + throw new Error(res.reason ?? unexpectedErrorMessage) } const { minSendable, maxSendable, ...leftOver } = res