hashicorp / go-multierror

A Go (golang) package for representing a list of errors as a single error.
Mozilla Public License 2.0
2.3k stars 123 forks source link

Allow marshalling a multi-error #15

Open ConradIrwin opened 7 years ago

ConradIrwin commented 7 years ago

Without this change any objects containing a multierror will fail to Marshal, though most objects that implement the Error() interface are safe (albeit not useful) to convert to JSON.

This pull request goes one step further and tries to make the JSON output somewhat useful, and defines a way to reconstruct a multierror that looks superficially similar to the original when Unmarshalled again.

hashicorp-cla commented 5 years ago

CLA assistant check
All committers have signed the CLA.

theherk commented 4 years ago

I don't think he is going to complete that cla, so I'm going to rework this so we can get it in.

theherk commented 4 years ago

This can be closed if #25 is merged.

ConradIrwin commented 4 years ago

fixed — sorry for missing that! Also happy for your version to be merged instead

theherk commented 4 years ago

Ah, no sweat. I didn't think you'd jump on it so quickly after two years, else I wouldn't have done that. Yeah, I hope you get merged up.

mentalisttraceur commented 4 years ago

Suggestion: marshal to just the inner list, instead of wrapping it in an {"errors": } object.

(Disclaimer: I'm not affiliated with Hashicorp in any way, I'm just a rando on the internet with Ideas(TM) and the hubris to think they're worth saying.)

Consider the difference between:

{ "response": null, "errors": [ "foo", "bar" ] }
{ "response": null, "errors": { "errors": [ "foo", "bar" ] } }

Basically:

  1. don't we usually marshal data like a list of errors into a bigger surrounding JSON blob anyway?

  2. isn't that surrounding context going to have its own "errors" key or other indicator that the data is a list of errors?