Skip to content

Commit

Permalink
Make panic recovery code consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Sep 30, 2024
1 parent 81fc283 commit 94d64db
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
8 changes: 5 additions & 3 deletions utils/crons/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log/slog"
"runtime/debug"
"sync"
"time"

Expand Down Expand Up @@ -117,9 +118,10 @@ func fireCron(rt *runtime.Runtime, name string, cronFunc Function, timeout time.

defer func() {
// catch any panics and recover
panicLog := recover()
if panicLog != nil {
slog.Error(fmt.Sprintf("panic running cron: %s", panicLog), "cron", name)
if panicVal := recover(); panicVal != nil {
debug.PrintStack()

slog.Error("panic running cron", "cron", name, "panic", panicVal)
}
}()

Expand Down
6 changes: 4 additions & 2 deletions web/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ func requestLogger(next http.Handler) http.Handler {
func panicRecovery(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rvr := recover(); rvr != nil {
if panicVal := recover(); panicVal != nil {
debug.PrintStack()
slog.Error("recovered from panic in web handling", "error", fmt.Sprint(rvr))

slog.Error("panic in web handling", "url", r.URL.String(), "panic", panicVal)

http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}()
Expand Down
6 changes: 3 additions & 3 deletions workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ func (w *Worker) handleTask(task *queues.Task) {

defer func() {
// catch any panics and recover
panicLog := recover()
if panicLog != nil {
if panicVal := recover(); panicVal != nil {
debug.PrintStack()
log.Error("panic handling task", "panic", panicLog, "task", string(task.Task))

log.Error("panic handling task", "task", string(task.Task), "panic", panicVal)
}

// mark our task as complete
Expand Down

0 comments on commit 94d64db

Please sign in to comment.