Skip to content

Commit

Permalink
Handle panics when comment section is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
bensadeh committed Nov 10, 2021
1 parent e8564ce commit 664b65e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Changelog
## 1.27
_WIP_
_2021-11-10_

**Bugfix**
- Fixed a bug where parsing of the comment section with 0 comments would cause a panic

## 1.26
_2021-11-06_
Expand Down
10 changes: 9 additions & 1 deletion comment/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func ToString(comments endpoints.Comments, config *core.Config, screenWidth int)
commentSectionScreenWidth := screenWidth - margins.CommentSectionLeftMargin

header := getHeader(comments, config)
firstCommentID := comments.Comments[0].ID
firstCommentID := getFirstCommentID(comments.Comments)

replies := ""

Expand All @@ -38,6 +38,14 @@ func ToString(comments endpoints.Comments, config *core.Config, screenWidth int)
return commentSection
}

func getFirstCommentID(comments []endpoints.Comments) int {
if len(comments) == 0 {
return 0
}

return comments[0].ID
}

func getHeader(c endpoints.Comments, config *core.Config) string {
headline := getHeadline(c.Title, config)
infoLine := getInfoLine(c.Points, c.User, c.TimeAgo, c.CommentsCount, c.ID)
Expand Down

0 comments on commit 664b65e

Please sign in to comment.