-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Users/mordvinx/testplane 404.new UI error boundaries (#630)
* feat(css): add min-width and min-height to app In the future, it will help to render ErrorBoundary to the full screen. * refactor(visual-checks): extract VisualCheckStickyHeader FC * refactor(test-steps): raise CollapsibleSection, extract TestStep FC * feat(error-handling): add ErrorHandling components * style(suite-title): remove unused interface * feat(test-step): add corrupted style for test step item * feat(error-handling): add usages of ErrorHandler component * refactor(error-handler): rename file * refactor(error-handler): reorganize components * feat(error-handler): add top level error handler * feat(error-handler): additional message * fix(icons): add new icons Не добавил сразу, потому что они под gitignore * feat(error-handler): change error font size from 15px to 13px * feat(error-handler): fallback components will take 100% of width * feat(error-handler): change data corruption fallback style * feat(error-handler): raise AssertViewResult error handling * feat(error-handler): reorganize fallbacks and add styling * feat(error-handling): clip stack if length is more than 50 lines * fix(imports): remove unused import * feat(error-handling): change icon * fix(styles): pretty width limitation for separator * refactor(error-info): remove stack clipping * feat(corrupted-test-step): update warning colors * refactor(error-actions): do not use internals * refactor(naming): rename ErrorHandler Root to Boundary * feat(error-handler): add recommended action for CardCrash * feat(error-boundary): add handling for any typed values * feat(error-handling): change data corrution fallback align * feat(error-handling): add handling for broken pages * feat(error-handling): full width buttons * fix(error-info): fix weird borders * feat(error-handling): set code block max height * fix(error-handling): fix mistypes * feat(tests): add svg import stub --------- Co-authored-by: = <=>
- Loading branch information
Showing
24 changed files
with
776 additions
and
249 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
101 changes: 101 additions & 0 deletions
101
lib/static/new-ui/features/error-handling/components/ErrorHandling/Boundary.tsx
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,101 @@ | ||
import React, {Component, DependencyList, ErrorInfo, ReactNode} from 'react'; | ||
import {BoundaryProps, BoundaryState} from './interfaces'; | ||
import {ErrorContextProvider} from './context'; | ||
|
||
export class Boundary extends Component<BoundaryProps, BoundaryState> { | ||
constructor(props: BoundaryProps) { | ||
super(props); | ||
this.state = { | ||
watchFor: props.watchFor, | ||
hasError: false, | ||
error: null, | ||
errorInfo: null | ||
}; | ||
} | ||
|
||
private static messageStyle = 'background: crimson; color: white;'; | ||
private static timestampStyle = 'color: gray; font-size: smaller'; | ||
|
||
private static isNothingChanged(prev?: DependencyList, next?: DependencyList): boolean { | ||
if (prev === next) { | ||
return true; | ||
} | ||
|
||
if (prev === undefined || next === undefined) { | ||
return false; | ||
} | ||
|
||
if (prev.length !== next.length) { | ||
return false; | ||
} | ||
|
||
return prev.every((item, index) => item === next[index]); | ||
} | ||
|
||
private restore(): void { | ||
this.setState({hasError: false, error: null}); | ||
} | ||
|
||
static getDerivedStateFromProps(nextProps: BoundaryProps, prevState: BoundaryState): null | BoundaryState { | ||
if (Boundary.isNothingChanged(prevState.watchFor, nextProps.watchFor)) { | ||
return null; | ||
} | ||
|
||
return {...prevState, error: null, hasError: false, errorInfo: null, watchFor: nextProps.watchFor}; | ||
} | ||
|
||
private componentDidCatchErrorInstance(error: Error, errorInfo: ErrorInfo): void { | ||
this.setState({hasError: true, error, errorInfo}); | ||
|
||
const timestamp = new Date().toTimeString(); | ||
|
||
console.groupCollapsed( | ||
`%cError boundary catched error named "${error.name}". See details below:` + '%c @ ' + timestamp, | ||
Boundary.messageStyle, | ||
Boundary.timestampStyle | ||
); | ||
console.error(error); | ||
console.error('Component stack: ', errorInfo.componentStack); | ||
console.groupEnd(); | ||
} | ||
|
||
private componentDidCatchSomethingWeird(notAnError: unknown, errorInfo: ErrorInfo): void { | ||
this.setState({hasError: true, error: new Error(`Unknown error, based on ${typeof notAnError} value provided.\nReceived value: ${notAnError}, which is not an Error instance.\nTry check your code for throwing ${typeof notAnError}s.`), errorInfo}); | ||
|
||
const timestamp = new Date().toTimeString(); | ||
|
||
console.groupCollapsed( | ||
`%cError boundary catched ${typeof notAnError} instead of Error class instance. Try check your code for throwing ${typeof notAnError}s. See details below:` + '%c @ ' + timestamp, | ||
Boundary.messageStyle, | ||
Boundary.timestampStyle | ||
); | ||
|
||
console.log(`Received ${typeof notAnError} value: `, notAnError); | ||
console.error('Component stack: ', errorInfo.componentStack); | ||
console.groupEnd(); | ||
} | ||
|
||
/** | ||
* @param _error - The value throwed from the child component. This is not necessarily an Error class instance because of the ability to throw anything in JS. | ||
* @param errorInfo - React specific object, contains useful componentStack parameter. | ||
*/ | ||
componentDidCatch(error: unknown, errorInfo: ErrorInfo): void { | ||
if (error instanceof Error) { | ||
return this.componentDidCatchErrorInstance(error, errorInfo); | ||
} | ||
|
||
return this.componentDidCatchSomethingWeird(error, errorInfo); | ||
} | ||
|
||
render(): ReactNode { | ||
if (this.state.hasError) { | ||
return ( | ||
<ErrorContextProvider value={{state: this.state, restore: this.restore.bind(this)}}> | ||
{this.props.fallback} | ||
</ErrorContextProvider> | ||
); | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
lib/static/new-ui/features/error-handling/components/ErrorHandling/actions.tsx
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,32 @@ | ||
import {ArrowsRotateLeft} from '@gravity-ui/icons'; | ||
import {Button, ButtonProps, Icon} from '@gravity-ui/uikit'; | ||
import React, {ReactNode} from 'react'; | ||
import GithubIcon from '../../../../../icons/github-icon.svg'; | ||
import {NEW_ISSUE_LINK} from '@/constants'; | ||
|
||
type ActionProps = Omit<ButtonProps, 'view' | 'onClick'>; | ||
|
||
function reportIssue(): void { | ||
window.open(NEW_ISSUE_LINK, '_blank'); | ||
} | ||
|
||
export function FileIssue(props: ActionProps): ReactNode { | ||
return <Button {...props} view="outlined" onClick={reportIssue}> | ||
<Button.Icon> | ||
<img src={GithubIcon} alt="icon" width={17} height={17} /> | ||
</Button.Icon> | ||
|
||
File an issue | ||
</Button>; | ||
} | ||
|
||
function reloadPage(): void { | ||
window.location.reload(); | ||
} | ||
|
||
export function ReloadPage(props: ActionProps): ReactNode { | ||
return <Button {...props} view="outlined" onClick={reloadPage}> | ||
<Icon data={ArrowsRotateLeft} /> | ||
Refresh this page | ||
</Button>; | ||
} |
16 changes: 16 additions & 0 deletions
16
lib/static/new-ui/features/error-handling/components/ErrorHandling/context.ts
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,16 @@ | ||
import React from 'react'; | ||
import {ErrorContext} from './interfaces'; | ||
|
||
const Context = React.createContext<ErrorContext| null>(null); | ||
|
||
export const ErrorContextProvider = Context.Provider; | ||
|
||
export const useErrorContext = (): ErrorContext => { | ||
const ctx = React.useContext(Context); | ||
|
||
if (ctx === null) { | ||
throw new Error('useErrorContext must be used within ErrorContextProvider'); | ||
} | ||
|
||
return ctx; | ||
}; |
Oops, something went wrong.