Fix wrong method in error message (#2065)
This commit is contained in:
parent
78f7e006d5
commit
40cd0ea422
@ -10,10 +10,11 @@ export const createInvoice = async ({ msats, description, expiry }, { socket, ru
|
|||||||
|
|
||||||
const url = `${agent.protocol}//${socket}/v1/invoice`
|
const url = `${agent.protocol}//${socket}/v1/invoice`
|
||||||
|
|
||||||
|
const method = 'POST'
|
||||||
let res
|
let res
|
||||||
try {
|
try {
|
||||||
res = await fetch(url, {
|
res = await fetch(url, {
|
||||||
method: 'POST',
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Rune: rune,
|
Rune: rune,
|
||||||
@ -41,8 +42,8 @@ export const createInvoice = async ({ msats, description, expiry }, { socket, ru
|
|||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
|
|
||||||
assertResponseOk(res)
|
assertResponseOk(res, { method })
|
||||||
assertContentTypeJson(res)
|
assertContentTypeJson(res, { method })
|
||||||
|
|
||||||
const inv = await res.json()
|
const inv = await res.json()
|
||||||
if (inv.error) {
|
if (inv.error) {
|
||||||
|
24
lib/url.js
24
lib/url.js
@ -213,22 +213,30 @@ export function parseNwcUrl (walletConnectUrl) {
|
|||||||
return params
|
return params
|
||||||
}
|
}
|
||||||
|
|
||||||
export function assertResponseOk (res) {
|
class ResponseAssertError extends Error {
|
||||||
if (!res.ok) {
|
constructor (res, { method } = {}) {
|
||||||
|
if (method) {
|
||||||
|
super(`${method} ${res.url}: ${res.status} ${res.statusText}`)
|
||||||
|
} else {
|
||||||
|
super(`${res.url}: ${res.status} ${res.statusText}`)
|
||||||
|
}
|
||||||
|
this.name = 'ResponseAssertError'
|
||||||
// consume response body to avoid memory leaks
|
// consume response body to avoid memory leaks
|
||||||
// see https://github.com/nodejs/node/issues/51162
|
// see https://github.com/nodejs/node/issues/51162
|
||||||
res.text().catch(() => {})
|
res.text().catch(() => {})
|
||||||
throw new Error(`POST ${res.url}: ${res.status} ${res.statusText}`)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function assertContentTypeJson (res) {
|
export function assertResponseOk (res, { method } = {}) {
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new ResponseAssertError(res, { method })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function assertContentTypeJson (res, { method } = {}) {
|
||||||
const contentType = res.headers.get('content-type')
|
const contentType = res.headers.get('content-type')
|
||||||
if (!contentType || !contentType.includes('application/json')) {
|
if (!contentType || !contentType.includes('application/json')) {
|
||||||
// consume response body to avoid memory leaks
|
throw new ResponseAssertError(res, { method })
|
||||||
// see https://github.com/nodejs/node/issues/51162
|
|
||||||
res.text().catch(() => {})
|
|
||||||
throw new Error(`POST ${res.url}: ${res.status} ${res.statusText}`)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,8 +35,9 @@ export async function getWallet ({ apiKey, currency }, { signal }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function request ({ apiKey, query, variables = {} }, { signal }) {
|
export async function request ({ apiKey, query, variables = {} }, { signal }) {
|
||||||
|
const method = 'POST'
|
||||||
const res = await fetchWithTimeout(galoyBlinkUrl, {
|
const res = await fetchWithTimeout(galoyBlinkUrl, {
|
||||||
method: 'POST',
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-API-KEY': apiKey
|
'X-API-KEY': apiKey
|
||||||
@ -45,8 +46,8 @@ export async function request ({ apiKey, query, variables = {} }, { signal }) {
|
|||||||
signal
|
signal
|
||||||
})
|
})
|
||||||
|
|
||||||
assertResponseOk(res)
|
assertResponseOk(res, { method })
|
||||||
assertContentTypeJson(res)
|
assertContentTypeJson(res, { method })
|
||||||
|
|
||||||
return res.json()
|
return res.json()
|
||||||
}
|
}
|
||||||
|
@ -27,10 +27,11 @@ export const createInvoice = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// call callback with amount and conditionally comment
|
// call callback with amount and conditionally comment
|
||||||
const res = await fetchWithTimeout(callbackUrl.toString(), { signal })
|
const method = 'GET'
|
||||||
|
const res = await fetchWithTimeout(callbackUrl.toString(), { method, signal })
|
||||||
|
|
||||||
assertResponseOk(res)
|
assertResponseOk(res, { method })
|
||||||
assertContentTypeJson(res)
|
assertContentTypeJson(res, { method })
|
||||||
|
|
||||||
const body = await res.json()
|
const body = await res.json()
|
||||||
if (body.status === 'ERROR') {
|
if (body.status === 'ERROR') {
|
||||||
|
@ -33,9 +33,10 @@ async function getWallet ({ url, adminKey, invoiceKey }, { signal }) {
|
|||||||
headers.append('Content-Type', 'application/json')
|
headers.append('Content-Type', 'application/json')
|
||||||
headers.append('X-Api-Key', adminKey || invoiceKey)
|
headers.append('X-Api-Key', adminKey || invoiceKey)
|
||||||
|
|
||||||
const res = await fetchWithTimeout(url + path, { method: 'GET', headers, signal })
|
const method = 'GET'
|
||||||
|
const res = await fetchWithTimeout(url + path, { method, headers, signal })
|
||||||
|
|
||||||
assertContentTypeJson(res)
|
assertContentTypeJson(res, { method })
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const errBody = await res.json()
|
const errBody = await res.json()
|
||||||
throw new Error(errBody.detail)
|
throw new Error(errBody.detail)
|
||||||
@ -55,9 +56,10 @@ async function postPayment (bolt11, { url, adminKey }, { signal }) {
|
|||||||
|
|
||||||
const body = JSON.stringify({ bolt11, out: true })
|
const body = JSON.stringify({ bolt11, out: true })
|
||||||
|
|
||||||
const res = await fetchWithTimeout(url + path, { method: 'POST', headers, body, signal })
|
const method = 'POST'
|
||||||
|
const res = await fetchWithTimeout(url + path, { method, headers, body, signal })
|
||||||
|
|
||||||
assertContentTypeJson(res)
|
assertContentTypeJson(res, { method })
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const errBody = await res.json()
|
const errBody = await res.json()
|
||||||
throw new Error(errBody.detail)
|
throw new Error(errBody.detail)
|
||||||
@ -75,9 +77,10 @@ async function getPayment (paymentHash, { url, adminKey }, { signal }) {
|
|||||||
headers.append('Content-Type', 'application/json')
|
headers.append('Content-Type', 'application/json')
|
||||||
headers.append('X-Api-Key', adminKey)
|
headers.append('X-Api-Key', adminKey)
|
||||||
|
|
||||||
const res = await fetchWithTimeout(url + path, { method: 'GET', headers, signal })
|
const method = 'GET'
|
||||||
|
const res = await fetchWithTimeout(url + path, { method, headers, signal })
|
||||||
|
|
||||||
assertContentTypeJson(res)
|
assertContentTypeJson(res, { method })
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const errBody = await res.json()
|
const errBody = await res.json()
|
||||||
throw new Error(errBody.detail)
|
throw new Error(errBody.detail)
|
||||||
|
@ -42,9 +42,10 @@ export async function createInvoice (
|
|||||||
}
|
}
|
||||||
|
|
||||||
let res
|
let res
|
||||||
|
const method = 'POST'
|
||||||
try {
|
try {
|
||||||
res = await fetch(`${agent.protocol}//${hostname}${path}`, {
|
res = await fetch(`${agent.protocol}//${hostname}${path}`, {
|
||||||
method: 'POST',
|
method,
|
||||||
headers,
|
headers,
|
||||||
agent,
|
agent,
|
||||||
body,
|
body,
|
||||||
@ -54,12 +55,12 @@ export async function createInvoice (
|
|||||||
if (err.name === 'AbortError') {
|
if (err.name === 'AbortError') {
|
||||||
// XXX node-fetch doesn't throw our custom TimeoutError but throws a generic error so we have to handle that manually.
|
// XXX node-fetch doesn't throw our custom TimeoutError but throws a generic error so we have to handle that manually.
|
||||||
// see https://github.com/node-fetch/node-fetch/issues/1462
|
// see https://github.com/node-fetch/node-fetch/issues/1462
|
||||||
throw new FetchTimeoutError('POST', url, WALLET_CREATE_INVOICE_TIMEOUT_MS)
|
throw new FetchTimeoutError(method, url, WALLET_CREATE_INVOICE_TIMEOUT_MS)
|
||||||
}
|
}
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
|
|
||||||
assertContentTypeJson(res)
|
assertContentTypeJson(res, { method })
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const errBody = await res.json()
|
const errBody = await res.json()
|
||||||
throw new Error(errBody.detail)
|
throw new Error(errBody.detail)
|
||||||
|
@ -22,15 +22,16 @@ export async function sendPayment (bolt11, { url, primaryPassword }, { signal })
|
|||||||
const body = new URLSearchParams()
|
const body = new URLSearchParams()
|
||||||
body.append('invoice', bolt11)
|
body.append('invoice', bolt11)
|
||||||
|
|
||||||
|
const method = 'POST'
|
||||||
const res = await fetchWithTimeout(url + path, {
|
const res = await fetchWithTimeout(url + path, {
|
||||||
method: 'POST',
|
method,
|
||||||
headers,
|
headers,
|
||||||
body,
|
body,
|
||||||
signal
|
signal
|
||||||
})
|
})
|
||||||
|
|
||||||
assertResponseOk(res)
|
assertResponseOk(res, { method })
|
||||||
assertContentTypeJson(res)
|
assertContentTypeJson(res, { method })
|
||||||
|
|
||||||
const payment = await res.json()
|
const payment = await res.json()
|
||||||
const preimage = payment.paymentPreimage
|
const preimage = payment.paymentPreimage
|
||||||
|
@ -31,16 +31,17 @@ export async function createInvoice (
|
|||||||
const hostname = url.replace(/^https?:\/\//, '').replace(/\/+$/, '')
|
const hostname = url.replace(/^https?:\/\//, '').replace(/\/+$/, '')
|
||||||
const agent = getAgent({ hostname })
|
const agent = getAgent({ hostname })
|
||||||
|
|
||||||
|
const method = 'POST'
|
||||||
const res = await fetchWithTimeout(`${agent.protocol}//${hostname}${path}`, {
|
const res = await fetchWithTimeout(`${agent.protocol}//${hostname}${path}`, {
|
||||||
method: 'POST',
|
method,
|
||||||
headers,
|
headers,
|
||||||
agent,
|
agent,
|
||||||
body,
|
body,
|
||||||
signal
|
signal
|
||||||
})
|
})
|
||||||
|
|
||||||
assertResponseOk(res)
|
assertResponseOk(res, { method })
|
||||||
assertContentTypeJson(res)
|
assertContentTypeJson(res, { method })
|
||||||
|
|
||||||
const payment = await res.json()
|
const payment = await res.json()
|
||||||
return payment.serialized
|
return payment.serialized
|
||||||
|
Loading…
x
Reference in New Issue
Block a user