Refactor Context Providers and Enhance Error Handling
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
- Replaced NotificationProvider with ActionsProvider in App.jsx to improve context management. - Introduced default actions in ActionsContext to streamline state handling for current objects and actions. - Improved error logging in ApiServerContext by consolidating error response handling and ensuring robust error reporting.
This commit is contained in:
parent
7574e1ef5b
commit
763bbf6f6e
@ -92,10 +92,10 @@ const AppContent = () => {
|
|||||||
<TooltipProvider>
|
<TooltipProvider>
|
||||||
<TableStateProvider>
|
<TableStateProvider>
|
||||||
<AppUpdateProvider>
|
<AppUpdateProvider>
|
||||||
|
<ActionsProvider>
|
||||||
<NotificationProvider>
|
<NotificationProvider>
|
||||||
<SpotlightProvider>
|
<SpotlightProvider>
|
||||||
<ActionsModalProvider>
|
<ActionsModalProvider>
|
||||||
<ActionsProvider>
|
|
||||||
<DashboardObjectToolsProvider>
|
<DashboardObjectToolsProvider>
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route
|
<Route
|
||||||
@ -173,10 +173,10 @@ const AppContent = () => {
|
|||||||
/>
|
/>
|
||||||
</Routes>
|
</Routes>
|
||||||
</DashboardObjectToolsProvider>
|
</DashboardObjectToolsProvider>
|
||||||
</ActionsProvider>
|
|
||||||
</ActionsModalProvider>
|
</ActionsModalProvider>
|
||||||
</SpotlightProvider>
|
</SpotlightProvider>
|
||||||
</NotificationProvider>
|
</NotificationProvider>
|
||||||
|
</ActionsProvider>
|
||||||
</AppUpdateProvider>
|
</AppUpdateProvider>
|
||||||
</TableStateProvider>
|
</TableStateProvider>
|
||||||
</TooltipProvider>
|
</TooltipProvider>
|
||||||
|
|||||||
@ -24,7 +24,20 @@ import { AuthContext } from './AuthContext'
|
|||||||
import { ElectronContext } from './ElectronContext'
|
import { ElectronContext } from './ElectronContext'
|
||||||
import { useNavigationTabs } from './NavigationTabsContext'
|
import { useNavigationTabs } from './NavigationTabsContext'
|
||||||
|
|
||||||
const ActionsContext = createContext()
|
const noop = () => {}
|
||||||
|
|
||||||
|
const defaultActions = {
|
||||||
|
currentObject: null,
|
||||||
|
currentObjectType: null,
|
||||||
|
setCurrentObject: noop,
|
||||||
|
setCurrentObjectType: noop,
|
||||||
|
callAction: noop,
|
||||||
|
clearAction: noop,
|
||||||
|
setOnModalOk: noop,
|
||||||
|
ctrlDown: false
|
||||||
|
}
|
||||||
|
|
||||||
|
const ActionsContext = createContext(defaultActions)
|
||||||
|
|
||||||
const ActionsProvider = ({ children }) => {
|
const ActionsProvider = ({ children }) => {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
|||||||
@ -1625,16 +1625,18 @@ const ApiServerProvider = ({ children }) => {
|
|||||||
const showError = useCallback(
|
const showError = useCallback(
|
||||||
(error, callback = null) => {
|
(error, callback = null) => {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
|
const responseData = error?.response?.data
|
||||||
|
const data =
|
||||||
|
responseData &&
|
||||||
|
typeof responseData === 'object' &&
|
||||||
|
!(responseData instanceof Blob)
|
||||||
|
? responseData
|
||||||
|
: null
|
||||||
|
if (data) {
|
||||||
console.error(error.response)
|
console.error(error.response)
|
||||||
console.error(error.response.data)
|
console.error(data)
|
||||||
console.error(error.response.data.code)
|
}
|
||||||
console.error(error.response.data.error)
|
const code = data?.code || 'UNKNOWN'
|
||||||
console.error(error.response.data.message)
|
|
||||||
console.error(error.response.data.status)
|
|
||||||
console.error(error.response.data.timestamp)
|
|
||||||
console.error(error.response.data.path)
|
|
||||||
console.error(error.response.data.method)
|
|
||||||
const code = error.response.data.code || 'UNKNOWN'
|
|
||||||
if (code == 'UNAUTHORIZED') {
|
if (code == 'UNAUTHORIZED') {
|
||||||
if (token) {
|
if (token) {
|
||||||
setUnauthenticated()
|
setUnauthenticated()
|
||||||
@ -1655,8 +1657,8 @@ const ApiServerProvider = ({ children }) => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var content = `Error ${error.code} (${error.status}): ${error.message}`
|
var content = `Error ${error.code} (${error.status}): ${error.message}`
|
||||||
if (error.response?.data?.error) {
|
if (data?.error) {
|
||||||
content = `${error.response?.data?.error} (${error.status})`
|
content = `${data.error} (${error.status})`
|
||||||
}
|
}
|
||||||
setErrorModalContent(content)
|
setErrorModalContent(content)
|
||||||
setRetryCallback(() => callback)
|
setRetryCallback(() => callback)
|
||||||
@ -2174,6 +2176,8 @@ const ApiServerProvider = ({ children }) => {
|
|||||||
return fileURL
|
return fileURL
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
if (err?.code === 'ERR_CANCELED') return
|
||||||
|
await normalizeAxiosBlobError(err)
|
||||||
showError(err, () => {
|
showError(err, () => {
|
||||||
fetchFileContent(file, download, onProgress)
|
fetchFileContent(file, download, onProgress)
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user