Skip to content

Commit

Permalink
fix: custom error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp committed Feb 6, 2023
1 parent ddc4251 commit c72fc6e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 20 deletions.
7 changes: 6 additions & 1 deletion runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export {
clearSensitiveCaches,
} from '@dhis2/app-service-offline'

export { PluginSender, PluginWrapper } from '@dhis2/app-service-plugin'
export {
PluginSender,
PluginWrapper,
usePluginErrorContext,
PluginErrorProvider,
} from '@dhis2/app-service-plugin'

export { Provider } from './Provider'
28 changes: 28 additions & 0 deletions services/plugin/src/PluginContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { createContext, Dispatch, useContext, useState } from 'react'

type PluginErrorContextType = {
onPluginError: any
setOnPluginError: any
}

const PluginErrorContext = createContext<PluginErrorContextType>({
onPluginError: null,
setOnPluginError: null,
})

const PluginErrorProvider = ({ children }: { children: React.Component }) => {
const [onPluginError, setOnPluginError] = useState(null)
const providerValue = {
onPluginError,
setOnPluginError,
}
return (
<PluginErrorContext.Provider value={providerValue}>
{children}
</PluginErrorContext.Provider>
)
}

const usePluginErrorContext = () => useContext(PluginErrorContext)

export { PluginErrorContext, PluginErrorProvider, usePluginErrorContext }
15 changes: 10 additions & 5 deletions services/plugin/src/PluginSender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,16 @@ export const PluginSender = ({

// if iframe has sent initial request, send new props
if (communicationReceived && iframeRef.current.contentWindow) {
postRobot.send(
iframeRef.current.contentWindow,
'updated',
iframeProps
)
postRobot
.send(
iframeRef.current.contentWindow,
'updated',
iframeProps
)
.catch((err) => {
// log postRobot errors, but do not bubble them up
console.error(err)
})
}
}
}, [propsToPass, communicationReceived, updateMissingProps])
Expand Down
39 changes: 25 additions & 14 deletions services/plugin/src/PluginWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import postRobot from 'post-robot'
import React, { useCallback, useEffect, useState } from 'react'
import { PluginErrorBoundary } from './PluginErrorBoundary'
import { usePluginErrorContext } from './PluginContext'

export const PluginWrapper = ({
requiredProps,
Expand All @@ -9,6 +10,7 @@ export const PluginWrapper = ({
requiredProps: [string]
children: any
}): any => {
const { setOnPluginError } = usePluginErrorContext()
const [propsFromParent, setPropsFromParent] = useState<any>()

const receivePropsFromParent = useCallback(
Expand All @@ -35,19 +37,27 @@ export const PluginWrapper = ({
} else {
updateMissingProps(null)
}

if (explictlyPassedProps.onError && setOnPluginError) {
setOnPluginError(() => (error: Error) => {
explictlyPassedProps.onError(error)
})
}
},
[requiredProps]
[requiredProps, setOnPluginError]
)

useEffect(() => {
// make first request for props to communicate that iframe is ready
postRobot
.send(window.top, 'getPropsFromParent')
.then(receivePropsFromParent)
.catch((err: Error) => {
console.error(err)
})
}, [receivePropsFromParent])
if (setOnPluginError) {
// make first request for props to communicate that iframe is ready
postRobot
.send(window.top, 'getPropsFromParent')
.then(receivePropsFromParent)
.catch((err: Error) => {
console.error(err)
})
}
}, [receivePropsFromParent, setOnPluginError])

useEffect(() => {
// set up listener to listen for subsequent sends from parent window
Expand All @@ -62,9 +72,10 @@ export const PluginWrapper = ({
return () => listener.cancel()
}, [receivePropsFromParent])

return (
<PluginErrorBoundary onCustomError={propsFromParent?.onError || null}>
{children({ ...propsFromParent })}
</PluginErrorBoundary>
)
return children({ ...propsFromParent })
// return (
// <PluginErrorBoundary onCustomError={propsFromParent?.onError || null}>
// {children({ ...propsFromParent })}
// </PluginErrorBoundary>
// )
}
2 changes: 2 additions & 0 deletions services/plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { PluginSender } from './PluginSender'

export { PluginWrapper } from './PluginWrapper'

export * from './PluginContext'

0 comments on commit c72fc6e

Please sign in to comment.