From b0819efaeae1fb03a199d936af4cc475e71fcaf7 Mon Sep 17 00:00:00 2001 From: zeripath Date: Sun, 21 Mar 2021 15:16:07 +0000 Subject: [PATCH] Place wrapper around comment as diff to catch panics (#15085) (#15086) * Place wrapper around comment as diff to prevent panics * propagate the panic up Signed-off-by: Andrew Thornton --- services/gitdiff/gitdiff.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index beb75c05e86..0d95817a44d 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -1282,6 +1282,16 @@ func CommentAsDiff(c *models.Comment) (*Diff, error) { // CommentMustAsDiff executes AsDiff and logs the error instead of returning func CommentMustAsDiff(c *models.Comment) *Diff { + if c == nil { + return nil + } + defer func() { + if err := recover(); err != nil { + stack := log.Stack(2) + log.Error("PANIC whilst retrieving diff for comment[%d] Error: %v\nStack: %s", c.ID, err, stack) + panic(fmt.Errorf("PANIC whilst retrieving diff for comment[%d] Error: %v\nStack: %s", c.ID, err, stack)) + } + }() diff, err := CommentAsDiff(c) if err != nil { log.Warn("CommentMustAsDiff: %v", err)