-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy patherror_boundary.tsx
More file actions
47 lines (35 loc) · 1.08 KB
/
error_boundary.tsx
File metadata and controls
47 lines (35 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) 2020-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react'
import {Utils} from './utils'
type State = {
hasError: boolean
}
type Props = {
children: React.ReactNode
}
export default class ErrorBoundary extends React.Component<Props, State> {
state = {hasError: false}
msg = 'Redirecting to error page...'
handleError = (): void => {
const url = Utils.getFrontendBaseURL(true) + '/error?id=unknown'
Utils.log('error boundary redirecting to ' + url)
window.location.replace(url)
}
static getDerivedStateFromError(/*error: Error*/): State {
return {hasError: true}
}
componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void {
Utils.logError(error + ': ' + errorInfo)
}
shouldComponentUpdate(): boolean {
return true
}
render(): React.ReactNode {
if (this.state.hasError) {
this.handleError()
return <span>{this.msg}</span>
}
return this.props.children
}
}