Skip to content

Commit

Permalink
native/Consumer: Replace panics on formatter errors with a simple fal…
Browse files Browse the repository at this point in the history
…lback text (#10)
  • Loading branch information
blaubaer authored Jul 7, 2023
1 parent a4ccfcb commit 473e192
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
10 changes: 5 additions & 5 deletions native/consumer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Writer struct {
Formatter formatter.Formatter

// Interceptor can be used to intercept the consumption of an event shortly
// before the actual consumption or directly afterwards. If nothing was
// before the actual consumption or directly afterward. If nothing was
// provided interceptor.Default will be used.
Interceptor interceptor.Interceptor

Expand All @@ -39,12 +39,12 @@ type Writer struct {
// additional performance costs.
Synchronized bool

// OnFormatError will be called if there as any kind of error while
// OnFormatError will be called if their as any kind of error while
// formatting an log.Event using the configured Formatter. If nothing was
// provided these errors will result in a panic.
// provided these errors will result in a fallback message of the event.
OnFormatError func(*Writer, io.Writer, error)

// OnColorInitializationError will be called if there as any kind of error
// OnColorInitializationError will be called if their as any kind of error
// while initialize the color support. If nothing was provided these errors
// will be silently swallowed.
OnColorInitializationError func(*Writer, io.Writer, error)
Expand Down Expand Up @@ -100,7 +100,7 @@ func (instance *Writer) Consume(event log.Event, source log.CoreLogger) {
if v := instance.OnFormatError; v != nil {
v(instance, out, err)
} else {
panic(fmt.Errorf("cannot format event %v: %w", event, err))
content = []byte(fmt.Sprintf("LOG_EVENT_FORMAT_ERROR (event: %v, error: %v)", event, err))
}
}

Expand Down
6 changes: 2 additions & 4 deletions native/consumer/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ func Test_Writer_Consume_panicsOnFormatErrors(t *testing.T) {
})
})

assert.Execution(t, func() {
instance.Consume(givenEvent, givenLogger)
}).WillPanicWith("cannot format event .+?: expected")
instance.Consume(givenEvent, givenLogger)

assert.ToBeEqual(t, "", givenOut.String())
assert.ToBeMatching(t, "^LOG_EVENT_FORMAT_ERROR \\(event: .+?, error: expected\\)$", givenOut.String())
}

func Test_Writer_Consume_callsHookOnFormatErrors(t *testing.T) {
Expand Down

0 comments on commit 473e192

Please sign in to comment.