Skip to content

Commit

Permalink
fix: fixed issue where ErrorBoundary would not have access to SaasPro…
Browse files Browse the repository at this point in the history
…vider context
  • Loading branch information
Pagebakers committed Oct 20, 2023
1 parent 0d76b3d commit 4a3c9dc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/tough-snakes-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@saas-ui/core': patch
'@saas-ui/react': patch
---

Fixed issue where ErrorBoundary would not have access to SaasProvider context
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@ import { Story, Meta } from '@storybook/react'
import { ErrorBoundary } from './'

import { EmptyState } from '../empty-state'
import { SaasProvider, useSaas } from '../provider'

export default {
title: 'Utilities/ErrorBoundary',
component: ErrorBoundary,
decorators: [
(Story) => {
return (
<SaasProvider onError={(err) => console.log('ERROR', err)}>
<Story />
</SaasProvider>
)
},
],
} as Meta

const Err = () => {
throw new Error('Test error')

return null
}

Expand Down
10 changes: 9 additions & 1 deletion packages/saas-ui-core/src/error-boundary/error-boundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SaasContext } from '../provider'
export interface ErrorBoundaryProps {
fallback?: React.ReactNode
children: React.ReactNode
onError?: (error: Error, errorInfo: React.ErrorInfo) => void
}

export interface ErrorBoundaryState {
Expand All @@ -19,6 +20,8 @@ export interface ErrorBoundaryState {
export class ErrorBoundary extends React.Component<ErrorBoundaryProps> {
state: ErrorBoundaryState

static contextType = SaasContext

declare context: React.ContextType<typeof SaasContext>

constructor(props: ErrorBoundaryProps) {
Expand All @@ -30,10 +33,15 @@ export class ErrorBoundary extends React.Component<ErrorBoundaryProps> {
return { error }
}

componentDidCatch(error: Error, errorInfo: any) {
onError = (error: Error, errorInfo: any) => {
this.props.onError?.(error, errorInfo)
this.context.onError?.(error, errorInfo)
}

componentDidCatch(error: Error, errorInfo: any) {
this.onError(error, errorInfo)
}

render() {
if (this.state.error) {
return this.props.fallback || <h1>Something went wrong.</h1>
Expand Down
10 changes: 3 additions & 7 deletions packages/saas-ui-core/src/provider/saas-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ interface SaasProviderProps extends ChakraProviderProps {
onError?: (error: Error, errorInfo: React.ErrorInfo) => void
}

export function SaasProvider({
theme,
linkComponent,
onError,
children,
...rest
}: SaasProviderProps) {
export function SaasProvider(props: SaasProviderProps) {
const { theme, linkComponent, onError, children, ...rest } = props

const context = {
linkComponent,
onError,
Expand Down

0 comments on commit 4a3c9dc

Please sign in to comment.