Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom error serialization support and provide sane defaults #78

Merged
merged 2 commits into from
Jul 2, 2018

Conversation

DusanKasan
Copy link
Contributor

@DusanKasan DusanKasan commented Jun 21, 2018

As per #9 and to offer a different approach from #11 and #35 this PR introduces custom error serialization with sane defaults without breaking the existing APIs.

This is just a first draft and is missing tests. Also, a bit of code duplication which I feel could be reduced but it serves to get the idea across.

It provides global error marshalling by exposing a var ErrorMarshalFunc func(error) interface{} in zerolog package that by default is a function that returns the passed argument. It should be overriden if you require custom error marshalling.

Then in every function that accept error or array of errors ErrorMarshalFunc is called on the error and then the result of it is processed like this:

  • if it implements LogObjectMarshaler, serialize it as an object
  • if it is a string serialize as a string
  • if it is an error, serialize as a string with the result of Error()
  • else serialize it as an interface

The side effect of this change is that the encoders don't need the AppendError/s methods anymore, as the errors are serialized directly to other types.

Copy link
Owner

@rs rs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with the solution.

@@ -18,6 +18,11 @@ var eventPool = &sync.Pool{
},
}

// ErrorMarshalFunc allows customization of global error marshaling
var ErrorMarshalFunc = func (err error) interface{} {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could have a nil default and skip the type assertion in the default state. It might improve performance when people not using this feature (to be tested though).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll write a benchmark and post results.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here's the benchmark https://gist.github.com/DusanKasan/6eb7ef0b4e784bbd26a32df0073ec9cc.

Results on my machine:

BenchmarkCalling-4                      20000000                66.7 ns/op            23 B/op          1 allocs/op
BenchmarkCheckingAndCalling-4           20000000                67.5 ns/op            23 B/op          1 allocs/op
BenchmarkCheckingAndNotCalling-4        20000000                65.8 ns/op            23 B/op          1 allocs/op

Nil handling would be a marginal speedup in default cases, however if you change the value from nil to something else you'll pay the price of that if that does the checking (again it's marginal so it shouldn't bother no one).

I don't know how to judge this really. However having a nil value as a default may be helpful also because user can't shoot himself in the foot now. In the original design if you assign nil to ErrorMarshalFunc it would panic on invocation.

Thoughts?

for _, err := range errs {
marshaled := ErrorMarshalFunc(err)
switch m := marshaled.(type) {
case LogObjectMarshaler:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you miss the nil case here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not, my reasoning is that nil will be handled by the default case in which it is serialized using the interface method (which will serialize it as "null").

@DusanKasan
Copy link
Contributor Author

@rs what is needed/missing for this to be merged? Would be nice to roll this out asap :)

@rs rs merged commit 1c6d99b into rs:master Jul 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants