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
This commit is contained in:
SatsAllDay 2024-03-24 14:25:14 -04:00 committed by GitHub
parent a7b0272200
commit 9bc95d4bb1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -33,10 +33,19 @@ export async function lnAddrOptions (addr) {
// support HTTP and HTTPS during development // support HTTP and HTTPS during development
protocol = process.env.PUBLIC_URL.split('://')[0] protocol = process.env.PUBLIC_URL.split('://')[0]
} }
const req = await fetch(`${protocol}://${domain}/.well-known/lnurlp/${name}`) const unexpectedErrorMessage = `An unexpected error occurred fetching the Lightning Address metadata for ${addr}. Check the address and try again.`
const res = await req.json() 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') { 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 const { minSendable, maxSendable, ...leftOver } = res