-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AXON-41] Implement AtlascodeErrorBoundary
* Add support to receive rendering error events from UI * Add AtlascodeErrorBoundary component * Put the new boundary to Settings page
- Loading branch information
1 parent
1a9d4eb
commit 3a11fea
Showing
17 changed files
with
280 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// TODO: move this with other analytics stuff into a separate folder | ||
// not doing it now to prevent too many import changes | ||
|
||
/** | ||
* Names of the channels used for routing analytics events in UI | ||
*/ | ||
export enum AnalyticsChannels { | ||
AtlascodeUiErrors = 'atlascode.ui.errors', | ||
} | ||
|
||
export type UIAnalyticsContext = { | ||
view: string; | ||
}; | ||
|
||
export type UIErrorInfo = UIAnalyticsContext & { | ||
stack: string; | ||
errorName: string; | ||
errorMessage: string; | ||
errorCause: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React, { useCallback } from 'react'; | ||
// eslint-disable-next-line no-restricted-imports | ||
import { AnalyticsErrorBoundary, AnalyticsListener, UIAnalyticsEvent } from '@atlaskit/analytics-next'; | ||
import { CommonActionType } from 'src/lib/ipc/fromUI/common'; | ||
import { AnalyticsChannels, UIAnalyticsContext } from 'src/analyticsTypes'; | ||
|
||
const STACK_LIMIT = 150; | ||
const COMPONENT_GLOBAL = 'global'; | ||
|
||
export type AtlascodeErrorBoundaryProps = { | ||
postMessageFunc: (action: any) => void; | ||
context: UIAnalyticsContext; | ||
children?: React.ReactNode; | ||
}; | ||
|
||
/** | ||
* Global error boundary for Atlascode UI components | ||
* | ||
* Put this somewhere close to the root of your component tree. | ||
* It will catch all rendering errors in the subtree and send them to the main process. | ||
* | ||
* Add `AnalyticsErrorBoundary` around the specific components you'd like to handle and track separately. | ||
* | ||
* @param {AtlascodeErrorBoundaryProps} props | ||
* @returns | ||
*/ | ||
export const AtlascodeErrorBoundary: React.FC<AtlascodeErrorBoundaryProps> = ({ | ||
children, | ||
postMessageFunc, | ||
context, | ||
}: AtlascodeErrorBoundaryProps) => { | ||
const onRenderError = useCallback( | ||
(event: UIAnalyticsEvent) => { | ||
const { error, info, ...rest } = event.payload.attributes; | ||
postMessageFunc({ | ||
type: CommonActionType.SendAnalytics, | ||
errorInfo: { | ||
...context, | ||
errorType: error.name, | ||
errorMessage: error.message, | ||
errorCause: error.cause, | ||
componentStack: simplifyStack( | ||
info.componentStack, | ||
(line) => line.match(/in (.*) \(created by (.*)\)/)?.[1], | ||
), | ||
stack: simplifyStack(error.stack, (line) => line.match(/at (.*) \((.*)\)/)?.[1]), | ||
...rest, | ||
}, | ||
}); | ||
}, | ||
[postMessageFunc, context], | ||
); | ||
|
||
return ( | ||
<AnalyticsListener channel={AnalyticsChannels.AtlascodeUiErrors} onEvent={onRenderError}> | ||
<AnalyticsErrorBoundary | ||
channel={AnalyticsChannels.AtlascodeUiErrors} | ||
data={{ component: COMPONENT_GLOBAL }} | ||
> | ||
{children} | ||
</AnalyticsErrorBoundary> | ||
</AnalyticsListener> | ||
); | ||
}; | ||
|
||
const simplifyStack = (stack: string, extractFunc: (line: string) => string | undefined) => { | ||
const line = stack.split('\n').map(extractFunc).filter(Boolean).join(' < '); | ||
|
||
return line.length > STACK_LIMIT ? line.substring(0, STACK_LIMIT) + '...' : line; | ||
}; |
Oops, something went wrong.