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:
parent
a7b0272200
commit
9bc95d4bb1
15
lib/lnurl.js
15
lib/lnurl.js
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user