forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery-error-view.js
101 lines (89 loc) · 2.31 KB
/
query-error-view.js
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
import React from 'react';
import PropTypes from 'prop-types';
import GithubLoginView from './github-login-view';
import ErrorView from './error-view';
import OfflineView from './offline-view';
export default class QueryErrorView extends React.Component {
static propTypes = {
error: PropTypes.shape({
name: PropTypes.string.isRequired,
message: PropTypes.string.isRequired,
stack: PropTypes.string.isRequired,
response: PropTypes.shape({
status: PropTypes.number.isRequired,
}),
responseText: PropTypes.string,
errors: PropTypes.arrayOf(PropTypes.shape({
message: PropTypes.string.isRequired,
})),
}).isRequired,
login: PropTypes.func.isRequired,
retry: PropTypes.func,
logout: PropTypes.func,
}
render() {
const e = this.props.error;
if (e.response) {
switch (e.response.status) {
case 401: return this.render401();
case 200:
// Do the default
break;
default: return this.renderUnknown(e.response, e.responseText);
}
}
if (e.errors) {
return this.renderGraphQLErrors(e.errors);
}
if (e.network) {
return this.renderNetworkError();
}
return (
<ErrorView
title={e.message}
descriptions={[e.stack]}
preformatted={true}
{...this.errorViewProps()}
/>
);
}
renderGraphQLErrors(errors) {
return (
<ErrorView
title="Query errors reported"
descriptions={errors.map(e => e.message)}
{...this.errorViewProps()}
/>
);
}
renderNetworkError() {
return <OfflineView retry={this.props.retry} />;
}
render401() {
return (
<div className="github-GithubLoginView-Container">
<GithubLoginView onLogin={this.props.login}>
<p>
The API endpoint returned a unauthorized error. Please try to re-authenticate with the endpoint.
</p>
</GithubLoginView>
</div>
);
}
renderUnknown(response, text) {
return (
<ErrorView
title={`Received an error response: ${response.status}`}
descriptions={[text]}
preformatted={true}
{...this.errorViewProps()}
/>
);
}
errorViewProps() {
return {
retry: this.props.retry,
logout: this.props.logout,
};
}
}