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>
|
||||
<TableStateProvider>
|
||||
<AppUpdateProvider>
|
||||
<ActionsProvider>
|
||||
<NotificationProvider>
|
||||
<SpotlightProvider>
|
||||
<ActionsModalProvider>
|
||||
<ActionsProvider>
|
||||
<DashboardObjectToolsProvider>
|
||||
<Routes>
|
||||
<Route
|
||||
@ -173,10 +173,10 @@ const AppContent = () => {
|
||||
/>
|
||||
</Routes>
|
||||
</DashboardObjectToolsProvider>
|
||||
</ActionsProvider>
|
||||
</ActionsModalProvider>
|
||||
</SpotlightProvider>
|
||||
</NotificationProvider>
|
||||
</ActionsProvider>
|
||||
</AppUpdateProvider>
|
||||
</TableStateProvider>
|
||||
</TooltipProvider>
|
||||
|
||||
@ -24,7 +24,20 @@ import { AuthContext } from './AuthContext'
|
||||
import { ElectronContext } from './ElectronContext'
|
||||
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 navigate = useNavigate()
|
||||
|
||||
@ -1625,16 +1625,18 @@ const ApiServerProvider = ({ children }) => {
|
||||
const showError = useCallback(
|
||||
(error, callback = null) => {
|
||||
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.data)
|
||||
console.error(error.response.data.code)
|
||||
console.error(error.response.data.error)
|
||||
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'
|
||||
console.error(data)
|
||||
}
|
||||
const code = data?.code || 'UNKNOWN'
|
||||
if (code == 'UNAUTHORIZED') {
|
||||
if (token) {
|
||||
setUnauthenticated()
|
||||
@ -1655,8 +1657,8 @@ const ApiServerProvider = ({ children }) => {
|
||||
return
|
||||
}
|
||||
var content = `Error ${error.code} (${error.status}): ${error.message}`
|
||||
if (error.response?.data?.error) {
|
||||
content = `${error.response?.data?.error} (${error.status})`
|
||||
if (data?.error) {
|
||||
content = `${data.error} (${error.status})`
|
||||
}
|
||||
setErrorModalContent(content)
|
||||
setRetryCallback(() => callback)
|
||||
@ -2174,6 +2176,8 @@ const ApiServerProvider = ({ children }) => {
|
||||
return fileURL
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
if (err?.code === 'ERR_CANCELED') return
|
||||
await normalizeAxiosBlobError(err)
|
||||
showError(err, () => {
|
||||
fetchFileContent(file, download, onProgress)
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user