Skip to content

Commit

Permalink
all: Recover from marshalling event data panic
Browse files Browse the repository at this point in the history
  • Loading branch information
vlasebian committed Dec 5, 2024
1 parent d17525f commit 9e374c3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions config/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4832,6 +4832,15 @@
"file": "pattern.go"
}
},
"error:pkg/events:marshal_data": {
"translations": {
"en": "failed to marshal data"
},
"description": {
"package": "pkg/events",
"file": "events.go"
}
},
"error:pkg/events:no_matching_events": {
"translations": {
"en": "no matching events for regexp `{regexp}`"
Expand Down
20 changes: 20 additions & 0 deletions pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
"strings"
"time"

"github.com/getsentry/sentry-go"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
sentryerrors "go.thethings.network/lorawan-stack/v3/pkg/errors/sentry"
"go.thethings.network/lorawan-stack/v3/pkg/goproto"
"go.thethings.network/lorawan-stack/v3/pkg/jsonpb"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
Expand Down Expand Up @@ -176,7 +178,25 @@ func New(ctx context.Context, name, description string, opts ...Option) Event {
return (&definition{name: name, description: description}).New(ctx, opts...)
}

var errMarshalData = errors.Define("marshal_data", "failed to marshal data")

func marshalData(data any) (anyPB *anypb.Any, err error) {
defer func() {
if p := recover(); p != nil {
if pErr, ok := p.(error); ok {
err = errMarshalData.WithCause(pErr)
} else {
err = errMarshalData.WithAttributes("panic", p)
}
event := sentryerrors.NewEvent(err)
sentry.CaptureEvent(event)
}
}()

return mustMarshalData(data)
}

func mustMarshalData(data any) (anyPB *anypb.Any, err error) {
if protoMessage, ok := data.(proto.Message); ok {
anyPB, err = anypb.New(protoMessage)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/webui/locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -2362,6 +2362,7 @@
"error:pkg/events/redis:channel_closed": "チャネルが閉じています",
"error:pkg/events/redis:unknown_encoding": "不明なエンコーディング",
"error:pkg/events:invalid_regexp": "無効な正規表現",
"error:pkg/events:marshal_data": "",
"error:pkg/events:no_matching_events": "正規表現`{regexp}`に一致するイベントがありません",
"error:pkg/events:unknown_event_name": "不明なイベント`{name}`",
"error:pkg/fetch:fetch_file": "ファイル `{filename}` を取得できません",
Expand Down

0 comments on commit 9e374c3

Please sign in to comment.