zapcore: recover panics from ObjectMarshaler and ArrayMarshaler (#1504)#1528
Open
rodionsteshenko wants to merge 1 commit intouber-go:masterfrom
Open
zapcore: recover panics from ObjectMarshaler and ArrayMarshaler (#1504)#1528rodionsteshenko wants to merge 1 commit intouber-go:masterfrom
rodionsteshenko wants to merge 1 commit intouber-go:masterfrom
Conversation
…ineMarshalerType Fixes uber-go#1504 Previously, panics in user-provided MarshalLogObject and MarshalLogArray implementations would crash the entire application. This is inconsistent with the existing panic recovery for Stringer (encodeStringer) and Error types. This change wraps ObjectMarshalerType, ArrayMarshalerType, and InlineMarshalerType field additions with panic recovery, converting panics to PANIC=<msg> error entries in the log context, consistent with the existing behavior for StringerType.
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1504
Problem
Panics in user-provided
MarshalLogObjectandMarshalLogArrayimplementations crash the entire application. This is inconsistent with the existing panic recovery forStringer(encodeStringer) andErrortypes, which gracefully convert panics toPANIC=<msg>error entries.Fix
Added three panic-recovering wrapper functions (
encodeObject,encodeArray,encodeInlinedObject) that mirror the existingencodeStringerpattern. These wrap the calls toenc.AddObject,enc.AddArray, andobj.MarshalLogObjectrespectively, converting any panic to aPANIC=<msg>error that gets logged as a<key>Errorfield.Affected field types:
ObjectMarshalerTypeArrayMarshalerTypeInlineMarshalerTypeTest
Added
panickyObjectandpanickyArraytest types plus 3 new test cases toTestFieldAddingErrorthat verify:ObjectMarshalerdoes not crash, producesPANIC=panic in MarshalLogObjectArrayMarshalerdoes not crash, producesPANIC=panic in MarshalLogArrayObjectMarshalerdoes not crash, producesPANIC=panic in MarshalLogObjectFull test suite passes on macOS ARM (Apple Silicon).