Skip to content

Commit 406f6cc

Browse files
committed
Added error display if a comment is unexpectedly null
1 parent 590fc70 commit 406f6cc

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

public/style.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,14 @@ span.control:hover {
296296
.Comment--dead > .Comment__content > .Comment__text {
297297
color: #ddd !important;
298298
}
299+
.Comment--error .Comment__meta {
300+
color: #f33;
301+
}
299302

300303
.UserProfile {
301304
padding-left: 1.25em;
302305
padding-top: 1em;
303306
}
304307
.UserProfile h4 {
305308
margin: 0 0 1em 0;
306-
}
309+
}

src/Comment.jsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ var Comment = React.createClass({
4545
},
4646

4747
componentDidUpdate(prevProps, prevState) {
48+
if (!this.state.comment) {
49+
return
50+
}
51+
4852
if (!prevState.comment.id) {
4953
// Register a newly-loaded comment with the thread store
5054
if (this.state.comment.id) {
@@ -117,6 +121,12 @@ var Comment = React.createClass({
117121
render() {
118122
var comment = this.state.comment
119123
var props = this.props
124+
if (!comment) {
125+
return this.renderError(comment, {
126+
id: this.props.id,
127+
className: 'Comment Comment--error Comment--level' + props.level
128+
})
129+
}
120130
// Render a placeholder while we're waiting for the comment to load
121131
if (!comment.id) { return this.renderCommentLoading(comment) }
122132
// Don't show dead coments or their children, when configured
@@ -162,4 +172,4 @@ var Comment = React.createClass({
162172
}
163173
})
164174

165-
module.exports = Comment
175+
module.exports = Comment

src/PermalinkedComment.jsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ var PermalinkedComment = React.createClass({
5252
},
5353

5454
componentWillUpdate(nextProps, nextState) {
55+
if (!nextState.comment) {
56+
return
57+
}
58+
5559
if (this.state.comment.id != nextState.comment.id) {
5660
if (!nextState.comment.deleted) {
5761
// Redirect to the appropriate route if a Comment "parent" link had a
@@ -95,6 +99,12 @@ var PermalinkedComment = React.createClass({
9599

96100
render() {
97101
var comment = this.state.comment
102+
if (!comment) {
103+
return this.renderError(comment, {
104+
id: this.props.params.id,
105+
className: 'Comment Comment--level0 Comment--error'
106+
})
107+
}
98108
// Render a placeholder while we're waiting for the comment to load
99109
if (!comment.id) { return this.renderCommentLoading(comment) }
100110
// Render a link to HN for deleted comments
@@ -130,4 +140,4 @@ var PermalinkedComment = React.createClass({
130140
}
131141
})
132142

133-
module.exports = PermalinkedComment
143+
module.exports = PermalinkedComment

src/mixins/CommentMixin.jsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ var CommentMixin = {
5757
</div>
5858
},
5959

60+
renderError(comment, options) {
61+
return <div className={options.className}>
62+
<div className="Comment__content">
63+
<div className="Comment__meta">
64+
[error] | comment is {JSON.stringify(comment)} | <a href={'https://news.ycombinator.com/item?id=' + options.id}>view on Hacker News</a>
65+
</div>
66+
</div>
67+
</div>
68+
},
69+
6070
renderCollapseControl(collapsed) {
6171
return <span className="Comment__collapse" onClick={this.toggleCollapse} onKeyPress={this.toggleCollapse} tabIndex="0">
6272
[{collapsed ? '+' : '–'}]
@@ -113,4 +123,4 @@ var CommentMixin = {
113123
}
114124
}
115125

116-
module.exports = CommentMixin
126+
module.exports = CommentMixin

0 commit comments

Comments
 (0)