Bubble up the message from an error body if it exists (#7202)
This commit is contained in:
@ -1,6 +1,5 @@
|
|||||||
import { DEV } from '@src/env'
|
import { DEV } from '@src/env'
|
||||||
import isomorphicFetch from 'isomorphic-fetch'
|
import isomorphicFetch from 'isomorphic-fetch'
|
||||||
|
|
||||||
import { isDesktop } from '@src/lib/isDesktop'
|
import { isDesktop } from '@src/lib/isDesktop'
|
||||||
|
|
||||||
// TODO I not sure this file should exist
|
// TODO I not sure this file should exist
|
||||||
@ -38,22 +37,21 @@ export default async function crossPlatformFetch<T>(
|
|||||||
return new Error('Failed to request endpoint: ' + url)
|
return new Error('Failed to request endpoint: ' + url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const data = (await response.json().catch((e) => e)) as T | Error
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
console.error(
|
console.error(
|
||||||
'Failed to request endpoint: ' + url,
|
`Failed to request endpoint: ${url}`,
|
||||||
JSON.stringify(response)
|
JSON.stringify(response),
|
||||||
)
|
data
|
||||||
return new Error(
|
|
||||||
'Failed to request endpoint: ' +
|
|
||||||
url +
|
|
||||||
' with status: ' +
|
|
||||||
response.status +
|
|
||||||
' ' +
|
|
||||||
response.statusText
|
|
||||||
)
|
)
|
||||||
|
const fallbackErrorMessage = `Failed to request endpoint: ${url} with status: ${response.status} ${response.statusText}`
|
||||||
|
const resolvedMessage =
|
||||||
|
data instanceof Object && 'message' in data
|
||||||
|
? data.message
|
||||||
|
: fallbackErrorMessage
|
||||||
|
return new Error(resolvedMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = (await response.json()) as T | Error
|
|
||||||
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user