-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
app-error.tsx
183 lines (157 loc) · 4.87 KB
/
app-error.tsx
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import * as React from 'react'
import {
Dialog,
DialogContent,
DialogFooter,
DefaultDialogFooter,
} from './dialog'
import {
dialogTransitionEnterTimeout,
dialogTransitionLeaveTimeout,
} from './app'
import { GitError } from '../lib/git/core'
import { GitError as GitErrorType } from 'dugite'
import { Popup, PopupType } from '../models/popup'
import { CSSTransitionGroup } from 'react-transition-group'
import { OkCancelButtonGroup } from './dialog/ok-cancel-button-group'
interface IAppErrorProps {
/** The list of queued, app-wide, errors */
readonly errors: ReadonlyArray<Error>
/**
* A callback which is used whenever a particular error
* has been shown to, and been dismissed by, the user.
*/
readonly onClearError: (error: Error) => void
readonly onShowPopup: (popupType: Popup) => void | undefined
}
interface IAppErrorState {
/** The currently displayed error or null if no error is shown */
readonly error: Error | null
/**
* Whether or not the dialog and its buttons are disabled.
* This is used when the dialog is transitioning out of view.
*/
readonly disabled: boolean
}
/**
* A component which renders application-wide errors as dialogs. Only one error
* is shown per dialog and if multiple errors are queued up they will be shown
* in the order they were queued.
*/
export class AppError extends React.Component<IAppErrorProps, IAppErrorState> {
public constructor(props: IAppErrorProps) {
super(props)
this.state = {
error: props.errors[0] || null,
disabled: false,
}
}
public componentWillReceiveProps(nextProps: IAppErrorProps) {
const error = nextProps.errors[0] || null
// We keep the currently shown error until it has disappeared
// from the first spot in the application error queue.
if (error !== this.state.error) {
this.setState({ error, disabled: false })
}
}
private onDismissed = () => {
const currentError = this.state.error
if (currentError) {
this.setState({ error: null, disabled: true })
// Give some time for the dialog to nicely transition
// out before we clear the error and, potentially, deal
// with the next error in the queue.
window.setTimeout(() => {
this.props.onClearError(currentError)
}, dialogTransitionLeaveTimeout)
}
}
private showPreferencesDialog = () => {
this.onDismissed()
//This is a hacky solution to resolve multiple dialog windows
//being open at the same time.
window.setTimeout(() => {
this.props.onShowPopup({ type: PopupType.Preferences })
}, dialogTransitionLeaveTimeout)
}
private renderGitErrorFooter(error: GitError) {
const gitErrorType = error.result.gitError
switch (gitErrorType) {
case GitErrorType.HTTPSAuthenticationFailed: {
return (
<DialogFooter>
<OkCancelButtonGroup
okButtonText="Close"
onOkButtonClick={this.onCloseButtonClick}
cancelButtonText={
__DARWIN__ ? 'Open Preferences' : 'Open options'
}
onCancelButtonClick={this.showPreferencesDialog}
/>
</DialogFooter>
)
}
default:
return <DefaultDialogFooter />
}
}
private renderErrorMessage(error: Error) {
let monospace = false
if (error instanceof GitError) {
// See getResultMessage in core.ts
// If the error message is the same as stderr or stdout then we know
// it's output from git and we'll display it in fixed-width font
if (
error.message === error.result.stderr ||
error.message === error.result.stdout
) {
monospace = true
}
}
const className = monospace ? 'monospace' : undefined
return <p className={className}>{error.message}</p>
}
private renderDialog() {
const error = this.state.error
if (!error) {
return null
}
return (
<Dialog
id="app-error"
type="error"
key="error"
title="Error"
dismissable={false}
onSubmit={this.onDismissed}
onDismissed={this.onDismissed}
disabled={this.state.disabled}
>
<DialogContent>{this.renderErrorMessage(error)}</DialogContent>
{this.renderFooter(error)}
</Dialog>
)
}
private onCloseButtonClick = (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault()
this.onDismissed()
}
private renderFooter(error: Error) {
if (error instanceof GitError) {
return this.renderGitErrorFooter(error)
}
return <DefaultDialogFooter />
}
public render() {
return (
<CSSTransitionGroup
transitionName="modal"
component="div"
transitionEnterTimeout={dialogTransitionEnterTimeout}
transitionLeaveTimeout={dialogTransitionLeaveTimeout}
>
{this.renderDialog()}
</CSSTransitionGroup>
)
}
}