don't rethrow
This commit is contained in:
parent
ee42d4095a
commit
ada7ae65ad
1 changed files with 28 additions and 34 deletions
|
|
@ -98,42 +98,36 @@ export const promisedRequest = async ({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = null
|
const response = await fetch(url, options)
|
||||||
try {
|
const data = await (async () => {
|
||||||
response = await fetch(url, options)
|
const [contentType] = response.headers
|
||||||
const data = await (async () => {
|
.get('content-type')
|
||||||
const [contentType] = response.headers
|
.split(';')
|
||||||
.get('content-type')
|
.map((x) => x.toLowerCase().trim())
|
||||||
.split(';')
|
const contentLength = parseInt(response.headers.get('content-length'))
|
||||||
.map((x) => x.toLowerCase().trim())
|
if (contentLength === 0) return null
|
||||||
const contentLength = parseInt(response.headers.get('content-length'))
|
|
||||||
if (contentLength === 0) return null
|
|
||||||
|
|
||||||
switch (contentType) {
|
switch (contentType) {
|
||||||
case 'text/plain':
|
case 'text/plain':
|
||||||
return await response.text()
|
return await response.text()
|
||||||
case 'application/json':
|
case 'application/json':
|
||||||
return await response.json()
|
return await response.json()
|
||||||
default:
|
default:
|
||||||
return await response.bytes()
|
return await response.bytes()
|
||||||
}
|
|
||||||
})()
|
|
||||||
|
|
||||||
const { ok, status } = response
|
|
||||||
|
|
||||||
if (ok) {
|
|
||||||
return { response, status, data }
|
|
||||||
} else {
|
|
||||||
throw new StatusCodeError(
|
|
||||||
response.status,
|
|
||||||
data,
|
|
||||||
{ url, options },
|
|
||||||
response,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
})()
|
||||||
if (error.name === 'StatusCodeError') throw error
|
|
||||||
throw new Error(error, { url, options }, response)
|
const { ok, status } = response
|
||||||
|
|
||||||
|
if (ok) {
|
||||||
|
return { response, status, data }
|
||||||
|
} else {
|
||||||
|
throw new StatusCodeError(
|
||||||
|
response.status,
|
||||||
|
data,
|
||||||
|
{ url, options },
|
||||||
|
response,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue