2
0

🔊 Add response debug log for failing requests without errors

This commit is contained in:
Baptiste Arnaud
2023-11-17 15:42:40 +01:00
parent 3a47a0fcbd
commit 5298538ecb
7 changed files with 17 additions and 12 deletions

View File

@@ -28,10 +28,11 @@ export const sendRequest = async <ResponseData>(
body?: Record<string, unknown> | FormData
}
| string
): Promise<{ data?: ResponseData; error?: Error }> => {
): Promise<{ data?: ResponseData; error?: Error; response?: Response }> => {
let response
try {
const url = typeof params === 'string' ? params : params.url
const response = await fetch(url, {
response = await fetch(url, {
method: typeof params === 'string' ? 'GET' : params.method,
mode: 'cors',
headers:
@@ -47,10 +48,10 @@ export const sendRequest = async <ResponseData>(
})
const data = await response.json()
if (!response.ok) throw 'error' in data ? data.error : data
return { data }
return { data, response }
} catch (e) {
console.error(e)
return { error: e as Error }
return { error: e as Error, response }
}
}