Skip to content

Commit

Permalink
test: Adding test for recoverer handler
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <bob@vibioh.fr>
  • Loading branch information
ViBiOh committed Jan 13, 2024
1 parent 02a1aef commit 61d9c93
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
9 changes: 8 additions & 1 deletion pkg/recoverer/recoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ func Error(err *error) {
return
}

*err = errors.Join(*err, WithStack(fmt.Errorf("recovered from panic: %s", r)))
recoverErr := WithStack(fmt.Errorf("recovered from panic: %s", r))

// Don't erase a potential error already present
if *err != nil {
*err = errors.Join(*err, recoverErr)
} else {
*err = recoverErr
}
}
}

Expand Down
28 changes: 26 additions & 2 deletions pkg/recoverer/recoverer_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package recoverer

import (
"fmt"
"errors"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestError(t *testing.T) {
t.Run("join", func(t *testing.T) {
t.Parallel()

err := fmt.Errorf("invalid")
err := errors.New("invalid")

func() {
defer Error(&err)
Expand All @@ -102,6 +102,30 @@ func TestError(t *testing.T) {
})
}

func TestHandler(t *testing.T) {
t.Parallel()

cases := map[string]struct{}{
"simple": {},
}

for intention := range cases {
t.Run(intention, func(t *testing.T) {
var err error

func() {
defer Handler(func(e error) {
err = e
})

panic("catch me if you can")
}()

assert.ErrorContains(t, err, "recovered")
})
}
}

func TestLogger(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 61d9c93

Please sign in to comment.