Add Error Normalization for Axios Blob Responses in ApiServerContext
All checks were successful
farmcontrol/farmcontrol-ui/pipeline/head This commit looks good
All checks were successful
farmcontrol/farmcontrol-ui/pipeline/head This commit looks good
- Introduced normalizeAxiosBlobError function to handle and normalize errors from Axios when the response data is a Blob. - Updated error handling in the ApiServerProvider to utilize the new normalization function, improving error reporting and user feedback. - Enhanced the Accept header in API requests to support both image and JSON responses, ensuring better compatibility with different content types.
This commit is contained in:
parent
39908c811a
commit
b925518e69
@ -45,6 +45,26 @@ const normalizeUserSettingsCategory = (category) =>
|
||||
? category
|
||||
: {}
|
||||
|
||||
const normalizeAxiosBlobError = async (error) => {
|
||||
const blob = error?.response?.data
|
||||
if (!(blob instanceof Blob)) {
|
||||
return error
|
||||
}
|
||||
|
||||
try {
|
||||
const text = await blob.text()
|
||||
try {
|
||||
error.response.data = JSON.parse(text)
|
||||
} catch {
|
||||
error.response.data = { error: text || 'Unknown error' }
|
||||
}
|
||||
} catch {
|
||||
error.response.data = { error: 'Unknown error' }
|
||||
}
|
||||
|
||||
return error
|
||||
}
|
||||
|
||||
const normalizeUserSettings = (settings = {}) => ({
|
||||
viewMode: normalizeUserSettingsCategory(settings?.viewMode),
|
||||
filterSidebarVisibility: normalizeUserSettingsCategory(
|
||||
@ -1717,7 +1737,7 @@ const ApiServerProvider = ({ children }) => {
|
||||
{
|
||||
params: { size },
|
||||
headers: {
|
||||
Accept: 'image/jpeg',
|
||||
Accept: 'image/jpeg, application/json',
|
||||
Authorization: `Bearer ${token}`
|
||||
},
|
||||
responseType: 'blob'
|
||||
@ -1728,12 +1748,13 @@ const ApiServerProvider = ({ children }) => {
|
||||
})
|
||||
return window.URL.createObjectURL(blob)
|
||||
} catch (err) {
|
||||
if (err.response.status === 404 && show404Error == false) {
|
||||
if (err.response?.status === 404 && show404Error == false) {
|
||||
return null
|
||||
}
|
||||
console.error(err)
|
||||
await normalizeAxiosBlobError(err)
|
||||
showError(err, () => {
|
||||
fetchFileThumbnail(file, size)
|
||||
fetchFileThumbnail(file, size, show404Error)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user