Skip to content

Commit

Permalink
refine error type treatment to allow custom serialization if it exist…
Browse files Browse the repository at this point in the history
…s/there are public fields
  • Loading branch information
ldemailly committed Nov 16, 2023
1 parent df6a201 commit 4cb8e0d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
21 changes: 17 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,23 +626,36 @@ func mapToString(s map[string]interface{}) string {
}
*/

func toJSON[T ValueTypes](v T) string {
func toJSON(v any) string {
e, isError := v.(error) // Remember and cast only once if this is an error type or not
if isError {
// Check for nil explicitly if v is an error
if e == nil {
return "null"
}

Check warning on line 635 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L634-L635

Added lines #L634 - L635 were not covered by tests
}
bytes, err := json.Marshal(v)
if err != nil {
return fmt.Sprintf("ERR marshaling %v: %v", v, err)
}

Check warning on line 640 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L639-L640

Added lines #L639 - L640 were not covered by tests
return string(bytes)
str := string(bytes)
// This is kinda hacky way to handle both structured and custom serialization errors, and
// struct with no public fields for which we need to call Error() to get a useful string.
if isError && str == "{}" {
return fmt.Sprintf("%q", e.Error())
}
return str
}

func (v ValueType[T]) String() string {
// if the type is numeric, use Sprint(v.val) otherwise use Sprintf("%q", v.Val) to quote it.
switch s := any(v.Val).(type) {
switch /*s :=*/ any(v.Val).(type) {
case bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64,
float32, float64:
return fmt.Sprint(v.Val)
/* It's all handled by json fallback now - todo test if this is/was cheaper?
case error: // struct with no public fields so we need to call Error() to get a string. otherwise we get {}
return fmt.Sprintf("%q", s.Error())
/* It's all handled by json fallback now - todo test if this is/was cheaper?
case []interface{}:
return arrayToString(s)
case map[string]interface{}:
Expand Down
31 changes: 27 additions & 4 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,13 +752,36 @@ func TestNoLevel(t *testing.T) {
}
}

type customError struct {
Msg string
Code int
}

func (e customError) Error() string {
return fmt.Sprintf("custom error %s (code %d)", e.Msg, e.Code)
}

func TestSerializationOfError(t *testing.T) {
err := fmt.Errorf("test error")
Errf("Error on purpose: %v", err)
S(Error, "Error on purpose", Any("err", err))
var err error
kv := Any("err", err)
kvStr := kv.StringValue()
expected := `"test error"`
expected := `null`
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
err = fmt.Errorf("test error")
Errf("Error on purpose: %v", err)
S(Error, "Error on purpose", Any("err", err))
kv = Any("err", err)
kvStr = kv.StringValue()
expected = `"test error"`
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
err = customError{Msg: "custom error", Code: 42}
kv = Any("err", err)
kvStr = kv.StringValue()
expected = `{"Msg":"custom error","Code":42}`
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
Expand Down

0 comments on commit 4cb8e0d

Please sign in to comment.