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