├── go.mod ├── go.sum ├── sort.go ├── flatten.go ├── prefix.go ├── group.go ├── sort_test.go ├── flatten_test.go ├── prefix_test.go ├── Makefile ├── group_test.go ├── append.go ├── format_test.go ├── format.go ├── append_test.go ├── multierror.go ├── multierror_test.go ├── README.md ├── .circleci └── config.yml └── LICENSE /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tetratelabs/multierror 2 | 3 | go 1.13 4 | 5 | require github.com/hashicorp/errwrap v1.1.0 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= 2 | github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= 3 | -------------------------------------------------------------------------------- /sort.go: -------------------------------------------------------------------------------- 1 | package multierror 2 | 3 | // Len implements sort.Interface function for length 4 | func (err Error) Len() int { 5 | return len(err.Errors) 6 | } 7 | 8 | // Swap implements sort.Interface function for swapping elements 9 | func (err Error) Swap(i, j int) { 10 | err.Errors[i], err.Errors[j] = err.Errors[j], err.Errors[i] 11 | } 12 | 13 | // Less implements sort.Interface function for determining order 14 | func (err Error) Less(i, j int) bool { 15 | return err.Errors[i].Error() < err.Errors[j].Error() 16 | } 17 | -------------------------------------------------------------------------------- /flatten.go: -------------------------------------------------------------------------------- 1 | package multierror 2 | 3 | // Flatten flattens the given error, merging any *Errors together into 4 | // a single *Error. 5 | func Flatten(err error) error { 6 | // If it isn't an *Error, just return the error as-is 7 | if _, ok := err.(*Error); !ok { 8 | return err 9 | } 10 | 11 | // Otherwise, make the result and flatten away! 12 | flatErr := new(Error) 13 | flatten(err, flatErr) 14 | return flatErr 15 | } 16 | 17 | func flatten(err error, flatErr *Error) { 18 | switch err := err.(type) { 19 | case *Error: 20 | for _, e := range err.Errors { 21 | flatten(e, flatErr) 22 | } 23 | default: 24 | flatErr.Errors = append(flatErr.Errors, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /prefix.go: -------------------------------------------------------------------------------- 1 | package multierror 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/hashicorp/errwrap" 7 | ) 8 | 9 | // Prefix is a helper function that will prefix some text 10 | // to the given error. If the error is a multierror.Error, then 11 | // it will be prefixed to each wrapped error. 12 | // 13 | // This is useful to use when appending multiple multierrors 14 | // together in order to give better scoping. 15 | func Prefix(err error, prefix string) error { 16 | if err == nil || err == (*Error)(nil) { 17 | return nil 18 | } 19 | 20 | format := fmt.Sprintf("%s {{err}}", prefix) 21 | switch err := err.(type) { 22 | case *Error: 23 | // Wrap each of the errors 24 | for i, e := range err.Errors { 25 | err.Errors[i] = errwrap.Wrapf(format, e) 26 | } 27 | 28 | return err 29 | default: 30 | return errwrap.Wrapf(format, err) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /group.go: -------------------------------------------------------------------------------- 1 | package multierror 2 | 3 | import "sync" 4 | 5 | // Group is a collection of goroutines which return errors that need to be 6 | // coalesced. 7 | type Group struct { 8 | mutex sync.Mutex 9 | err *Error 10 | wg sync.WaitGroup 11 | } 12 | 13 | // Go calls the given function in a new goroutine. 14 | // 15 | // If the function returns an error it is added to the group multierror which 16 | // is returned by Wait. 17 | func (g *Group) Go(f func() error) { 18 | g.wg.Add(1) 19 | 20 | go func() { 21 | defer g.wg.Done() 22 | 23 | if err := f(); err != nil { 24 | g.mutex.Lock() 25 | g.err = Append(g.err, err) 26 | g.mutex.Unlock() 27 | } 28 | }() 29 | } 30 | 31 | // Wait blocks until all function calls from the Go method have returned, then 32 | // returns the multierror. 33 | func (g *Group) Wait() *Error { 34 | g.wg.Wait() 35 | g.mutex.Lock() 36 | defer g.mutex.Unlock() 37 | return g.err 38 | } 39 | -------------------------------------------------------------------------------- /sort_test.go: -------------------------------------------------------------------------------- 1 | package multierror 2 | 3 | import ( 4 | "errors" 5 | "reflect" 6 | "sort" 7 | "testing" 8 | ) 9 | 10 | func TestSortSingle(t *testing.T) { 11 | errFoo := errors.New("foo") 12 | 13 | expected := []error{ 14 | errFoo, 15 | } 16 | 17 | err := &Error{ 18 | Errors: []error{ 19 | errFoo, 20 | }, 21 | } 22 | 23 | sort.Sort(err) 24 | if !reflect.DeepEqual(err.Errors, expected) { 25 | t.Fatalf("bad: %#v", err) 26 | } 27 | } 28 | 29 | func TestSortMultiple(t *testing.T) { 30 | errBar := errors.New("bar") 31 | errBaz := errors.New("baz") 32 | errFoo := errors.New("foo") 33 | 34 | expected := []error{ 35 | errBar, 36 | errBaz, 37 | errFoo, 38 | } 39 | 40 | err := &Error{ 41 | Errors: []error{ 42 | errFoo, 43 | errBar, 44 | errBaz, 45 | }, 46 | } 47 | 48 | sort.Sort(err) 49 | if !reflect.DeepEqual(err.Errors, expected) { 50 | t.Fatalf("bad: %#v", err) 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /flatten_test.go: -------------------------------------------------------------------------------- 1 | package multierror 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "reflect" 7 | "testing" 8 | ) 9 | 10 | func TestFlatten(t *testing.T) { 11 | original := &Error{ 12 | Errors: []error{ 13 | errors.New("one"), 14 | &Error{ 15 | Errors: []error{ 16 | errors.New("two"), 17 | &Error{ 18 | Errors: []error{ 19 | errors.New("three"), 20 | }, 21 | }, 22 | }, 23 | }, 24 | }, 25 | } 26 | 27 | expected := `3 errors occurred: 28 | * one 29 | * two 30 | * three 31 | 32 | ` 33 | actual := fmt.Sprintf("%s", SetFormatter(Flatten(original), ListFormatFunc)) 34 | 35 | if expected != actual { 36 | t.Fatalf("expected: %s, got: %s", expected, actual) 37 | } 38 | } 39 | 40 | func TestFlatten_nonError(t *testing.T) { 41 | err := errors.New("foo") 42 | actual := Flatten(err) 43 | if !reflect.DeepEqual(actual, err) { 44 | t.Fatalf("bad: %#v", actual) 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /prefix_test.go: -------------------------------------------------------------------------------- 1 | package multierror 2 | 3 | import ( 4 | "errors" 5 | "testing" 6 | ) 7 | 8 | func TestPrefix_Error(t *testing.T) { 9 | original := &Error{ 10 | Errors: []error{errors.New("foo")}, 11 | } 12 | 13 | result := Prefix(original, "bar") 14 | if result.(*Error).Errors[0].Error() != "bar foo" { 15 | t.Fatalf("bad: %s", result) 16 | } 17 | } 18 | 19 | func TestPrefix_NilError(t *testing.T) { 20 | var mErr *Error 21 | result := Prefix(mErr, "bar") 22 | if result != nil { 23 | t.Fatalf("bad: %#v", result) 24 | } 25 | 26 | var err error 27 | result = Prefix(err, "bar") 28 | if result != nil { 29 | t.Fatalf("bad: %#v", result) 30 | } 31 | } 32 | 33 | func TestPrefix_NonError(t *testing.T) { 34 | original := errors.New("foo") 35 | result := Prefix(original, "bar") 36 | if result == nil { 37 | t.Fatal("error result was nil") 38 | } 39 | if result.Error() != "bar foo" { 40 | t.Fatalf("bad: %s", result) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | TEST?=./... 2 | 3 | default: test 4 | 5 | # test runs the test suite and vets the code. 6 | test: generate 7 | @echo "==> Running tests..." 8 | @go list $(TEST) \ 9 | | grep -v "/vendor/" \ 10 | | xargs -n1 go test -timeout=60s -parallel=10 ${TESTARGS} 11 | 12 | # testrace runs the race checker 13 | testrace: generate 14 | @echo "==> Running tests (race)..." 15 | @go list $(TEST) \ 16 | | grep -v "/vendor/" \ 17 | | xargs -n1 go test -timeout=60s -race ${TESTARGS} 18 | 19 | # updatedeps installs all the dependencies needed to run and build. 20 | updatedeps: 21 | @sh -c "'${CURDIR}/scripts/deps.sh' '${NAME}'" 22 | 23 | # generate runs `go generate` to build the dynamically generated source files. 24 | generate: 25 | @echo "==> Generating..." 26 | @find . -type f -name '.DS_Store' -delete 27 | @go list ./... \ 28 | | grep -v "/vendor/" \ 29 | | xargs -n1 go generate 30 | 31 | .PHONY: default test testrace updatedeps generate 32 | -------------------------------------------------------------------------------- /group_test.go: -------------------------------------------------------------------------------- 1 | package multierror 2 | 3 | import ( 4 | "errors" 5 | "strings" 6 | "testing" 7 | ) 8 | 9 | func TestGroup(t *testing.T) { 10 | err1 := errors.New("group_test: 1") 11 | err2 := errors.New("group_test: 2") 12 | 13 | cases := []struct { 14 | errs []error 15 | nilResult bool 16 | }{ 17 | {errs: []error{}, nilResult: true}, 18 | {errs: []error{nil}, nilResult: true}, 19 | {errs: []error{err1}}, 20 | {errs: []error{err1, nil}}, 21 | {errs: []error{err1, nil, err2}}, 22 | } 23 | 24 | for _, tc := range cases { 25 | var g Group 26 | 27 | for _, err := range tc.errs { 28 | err := err 29 | g.Go(func() error { return err }) 30 | 31 | } 32 | 33 | gErr := g.Wait() 34 | if gErr != nil { 35 | for i := range tc.errs { 36 | if tc.errs[i] != nil && !strings.Contains(gErr.Error(), tc.errs[i].Error()) { 37 | t.Fatalf("expected error to contain %q, actual: %v", tc.errs[i].Error(), gErr) 38 | } 39 | } 40 | } else if !tc.nilResult { 41 | t.Fatalf("Group.Wait() should not have returned nil for errs: %v", tc.errs) 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /append.go: -------------------------------------------------------------------------------- 1 | package multierror 2 | 3 | // Append is a helper function that will append more errors 4 | // onto an Error in order to create a larger multi-error. 5 | // 6 | // If err is not a multierror.Error, then it will be turned into 7 | // one. If any of the errs are multierr.Error, they will be flattened 8 | // one level into err. 9 | // Any nil errors within errs will be ignored. If err is nil, a new 10 | // *Error will be returned. 11 | func Append(err error, errs ...error) *Error { 12 | switch err := err.(type) { 13 | case *Error: 14 | // Typed nils can reach here, so initialize if we are nil 15 | if err == nil { 16 | err = new(Error) 17 | } 18 | 19 | // Go through each error and flatten 20 | for _, e := range errs { 21 | switch e := e.(type) { 22 | case *Error: 23 | if e != nil { 24 | err.Errors = append(err.Errors, e.Errors...) 25 | } 26 | default: 27 | if e != nil { 28 | err.Errors = append(err.Errors, e) 29 | } 30 | } 31 | } 32 | 33 | return err 34 | default: 35 | newErrs := make([]error, 0, len(errs)+1) 36 | if err != nil { 37 | newErrs = append(newErrs, err) 38 | } 39 | newErrs = append(newErrs, errs...) 40 | 41 | return Append(&Error{}, newErrs...) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /format_test.go: -------------------------------------------------------------------------------- 1 | package multierror 2 | 3 | import ( 4 | "errors" 5 | "testing" 6 | ) 7 | 8 | func TestListFormatFuncSingle(t *testing.T) { 9 | expected := `1 error occurred: 10 | * foo 11 | 12 | ` 13 | 14 | errors := []error{ 15 | errors.New("foo"), 16 | } 17 | 18 | actual := ListFormatFunc(errors) 19 | if actual != expected { 20 | t.Fatalf("bad: %#v", actual) 21 | } 22 | } 23 | 24 | func TestListFormatFuncMultiple(t *testing.T) { 25 | expected := `2 errors occurred: 26 | * foo 27 | * bar 28 | 29 | ` 30 | 31 | errors := []error{ 32 | errors.New("foo"), 33 | errors.New("bar"), 34 | } 35 | 36 | actual := ListFormatFunc(errors) 37 | if actual != expected { 38 | t.Fatalf("bad: %#v", actual) 39 | } 40 | } 41 | 42 | func TestLineFormatFuncSingle(t *testing.T) { 43 | expected := `foo` 44 | 45 | errors := []error{ 46 | errors.New("foo"), 47 | } 48 | 49 | actual := LineErrorFormatFunc(errors) 50 | if actual != expected { 51 | t.Fatalf("bad: %#v", actual) 52 | } 53 | } 54 | 55 | func TestLineFormatFuncMultiple(t *testing.T) { 56 | expected := `2 errors occurred: foo; bar` 57 | 58 | errors := []error{ 59 | errors.New("foo"), 60 | errors.New("bar"), 61 | } 62 | 63 | actual := LineErrorFormatFunc(errors) 64 | if actual != expected { 65 | t.Fatalf("bad: %#v", actual) 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /format.go: -------------------------------------------------------------------------------- 1 | package multierror 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | ) 7 | 8 | // ErrorFormatFunc is a function callback that is called by Error to 9 | // turn the list of errors into a string. 10 | type ErrorFormatFunc func([]error) string 11 | 12 | // ListFormatFunc is a basic formatter that outputs the number of errors 13 | // that occurred along with a bullet point list of the errors. 14 | // 15 | // Typically this format is used for CLI exit responses. 16 | func ListFormatFunc(es []error) string { 17 | if len(es) == 1 { 18 | return fmt.Sprintf("1 error occurred:\n\t* %s\n\n", es[0]) 19 | } 20 | 21 | points := make([]string, len(es)) 22 | for i, err := range es { 23 | points[i] = fmt.Sprintf("* %s", err) 24 | } 25 | 26 | return fmt.Sprintf( 27 | "%d errors occurred:\n\t%s\n\n", 28 | len(es), strings.Join(points, "\n\t")) 29 | } 30 | 31 | // LineErrorFormatFunc is a basic formatter that outputs the number of errors 32 | // that occurred along with all errors on a single line. 33 | // 34 | // Typically this format is used for logs. 35 | func LineErrorFormatFunc(es []error) string { 36 | if len(es) == 1 { 37 | return es[0].Error() 38 | } 39 | 40 | var b strings.Builder 41 | for _, err := range es { 42 | b.WriteString(err.Error()) 43 | b.WriteString("; ") 44 | } 45 | return fmt.Sprintf("%d errors occurred: %s", len(es), b.String()[:b.Len()-2]) 46 | } 47 | -------------------------------------------------------------------------------- /append_test.go: -------------------------------------------------------------------------------- 1 | package multierror 2 | 3 | import ( 4 | "errors" 5 | "testing" 6 | ) 7 | 8 | func TestAppend_Error(t *testing.T) { 9 | original := &Error{ 10 | Errors: []error{errors.New("foo")}, 11 | } 12 | 13 | result := Append(original, errors.New("bar")) 14 | if len(result.Errors) != 2 { 15 | t.Fatalf("wrong len: %d", len(result.Errors)) 16 | } 17 | 18 | original = &Error{} 19 | result = Append(original, errors.New("bar")) 20 | if len(result.Errors) != 1 { 21 | t.Fatalf("wrong len: %d", len(result.Errors)) 22 | } 23 | 24 | // Test when a typed nil is passed 25 | var e *Error 26 | result = Append(e, errors.New("baz")) 27 | if len(result.Errors) != 1 { 28 | t.Fatalf("wrong len: %d", len(result.Errors)) 29 | } 30 | 31 | // Test flattening 32 | original = &Error{ 33 | Errors: []error{errors.New("foo")}, 34 | } 35 | 36 | result = Append(original, Append(nil, errors.New("foo"), errors.New("bar"))) 37 | if len(result.Errors) != 3 { 38 | t.Fatalf("wrong len: %d", len(result.Errors)) 39 | } 40 | } 41 | 42 | func TestAppend_NilError(t *testing.T) { 43 | var err error 44 | result := Append(err, errors.New("bar")) 45 | if len(result.Errors) != 1 { 46 | t.Fatalf("wrong len: %d", len(result.Errors)) 47 | } 48 | } 49 | 50 | func TestAppend_NilErrorArg(t *testing.T) { 51 | var err error 52 | var nilErr *Error 53 | result := Append(err, nilErr) 54 | if len(result.Errors) != 0 { 55 | t.Fatalf("wrong len: %d", len(result.Errors)) 56 | } 57 | } 58 | 59 | func TestAppend_NilErrorIfaceArg(t *testing.T) { 60 | var err error 61 | var nilErr error 62 | result := Append(err, nilErr) 63 | if len(result.Errors) != 0 { 64 | t.Fatalf("wrong len: %d", len(result.Errors)) 65 | } 66 | } 67 | 68 | func TestAppend_NonError(t *testing.T) { 69 | original := errors.New("foo") 70 | result := Append(original, errors.New("bar")) 71 | if len(result.Errors) != 2 { 72 | t.Fatalf("wrong len: %d", len(result.Errors)) 73 | } 74 | } 75 | 76 | func TestAppend_NonError_Error(t *testing.T) { 77 | original := errors.New("foo") 78 | result := Append(original, Append(nil, errors.New("bar"))) 79 | if len(result.Errors) != 2 { 80 | t.Fatalf("wrong len: %d", len(result.Errors)) 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /multierror.go: -------------------------------------------------------------------------------- 1 | package multierror 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | ) 7 | 8 | // Error is an error type to track multiple errors. This is used to 9 | // accumulate errors in cases and return them as a single "error". 10 | type Error struct { 11 | Errors []error 12 | ErrorFormat ErrorFormatFunc 13 | } 14 | 15 | func (e *Error) Error() string { 16 | fn := e.ErrorFormat 17 | if fn == nil { 18 | fn = LineErrorFormatFunc 19 | } 20 | 21 | return fn(e.Errors) 22 | } 23 | 24 | // ErrorOrNil returns an error interface if this Error represents 25 | // a list of errors, or returns nil if the list of errors is empty. This 26 | // function is useful at the end of accumulation to make sure that the value 27 | // returned represents the existence of errors. 28 | func (e *Error) ErrorOrNil() error { 29 | if e == nil { 30 | return nil 31 | } 32 | if len(e.Errors) == 0 { 33 | return nil 34 | } 35 | 36 | return e 37 | } 38 | 39 | func (e *Error) GoString() string { 40 | return fmt.Sprintf("*%#v", *e) 41 | } 42 | 43 | // WrappedErrors returns the list of errors that this Error is wrapping. It is 44 | // an implementation of the errwrap.Wrapper interface so that multierror.Error 45 | // can be used with that library. 46 | // 47 | // This method is not safe to be called concurrently. Unlike accessing the 48 | // Errors field directly, this function also checks if the multierror is nil to 49 | // prevent a null-pointer panic. It satisfies the errwrap.Wrapper interface. 50 | func (e *Error) WrappedErrors() []error { 51 | if e == nil { 52 | return nil 53 | } 54 | return e.Errors 55 | } 56 | 57 | // Unwrap returns an error from Error (or nil if there are no errors). 58 | // This error returned will further support Unwrap to get the next error, 59 | // etc. The order will match the order of Errors in the multierror.Error 60 | // at the time of calling. 61 | // 62 | // The resulting error supports errors.As/Is/Unwrap so you can continue 63 | // to use the stdlib errors package to introspect further. 64 | // 65 | // This will perform a shallow copy of the errors slice. Any errors appended 66 | // to this error after calling Unwrap will not be available until a new 67 | // Unwrap is called on the multierror.Error. 68 | func (e *Error) Unwrap() error { 69 | // If we have no errors then we do nothing 70 | if e == nil || len(e.Errors) == 0 { 71 | return nil 72 | } 73 | 74 | // If we have exactly one error, we can just return that directly. 75 | if len(e.Errors) == 1 { 76 | return e.Errors[0] 77 | } 78 | 79 | // Shallow copy the slice 80 | errs := make([]error, len(e.Errors)) 81 | copy(errs, e.Errors) 82 | return chain(errs) 83 | } 84 | 85 | // chain implements the interfaces necessary for errors.Is/As/Unwrap to 86 | // work in a deterministic way with multierror. A chain tracks a list of 87 | // errors while accounting for the current represented error. This lets 88 | // Is/As be meaningful. 89 | // 90 | // Unwrap returns the next error. In the cleanest form, Unwrap would return 91 | // the wrapped error here but we can't do that if we want to properly 92 | // get access to all the errors. Instead, users are recommended to use 93 | // Is/As to get the correct error type out. 94 | // 95 | // Precondition: []error is non-empty (len > 0) 96 | type chain []error 97 | 98 | // Error implements the error interface 99 | func (e chain) Error() string { 100 | return e[0].Error() 101 | } 102 | 103 | // Unwrap implements errors.Unwrap by returning the next error in the 104 | // chain or nil if there are no more errors. 105 | func (e chain) Unwrap() error { 106 | if len(e) == 1 { 107 | return nil 108 | } 109 | 110 | return e[1:] 111 | } 112 | 113 | // As implements errors.As by attempting to map to the current value. 114 | func (e chain) As(target interface{}) bool { 115 | return errors.As(e[0], target) 116 | } 117 | 118 | // Is implements errors.Is by comparing the current value directly. 119 | func (e chain) Is(target error) bool { 120 | return errors.Is(e[0], target) 121 | } 122 | 123 | // SetFormatter if provided with a multierror will update the multierror 124 | // serialization function with the provided ErrorFormatFunc. 125 | // 126 | // This method is not safe to be called concurrently. 127 | func SetFormatter(err error, fn ErrorFormatFunc) error { 128 | if e, ok := err.(*Error); ok { 129 | // check for typed nil 130 | if e != nil { 131 | e.ErrorFormat = fn 132 | } 133 | } 134 | return err 135 | } 136 | -------------------------------------------------------------------------------- /multierror_test.go: -------------------------------------------------------------------------------- 1 | package multierror 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "reflect" 7 | "testing" 8 | ) 9 | 10 | func TestError_Impl(t *testing.T) { 11 | var _ error = new(Error) 12 | } 13 | 14 | func TestErrorError_custom(t *testing.T) { 15 | errors := []error{ 16 | errors.New("foo"), 17 | errors.New("bar"), 18 | } 19 | 20 | fn := func(es []error) string { 21 | return "foo" 22 | } 23 | 24 | multi := &Error{Errors: errors, ErrorFormat: fn} 25 | if multi.Error() != "foo" { 26 | t.Fatalf("bad: %s", multi.Error()) 27 | } 28 | } 29 | 30 | func TestErrorError_default(t *testing.T) { 31 | expected := `2 errors occurred: foo; bar` 32 | 33 | errors := []error{ 34 | errors.New("foo"), 35 | errors.New("bar"), 36 | } 37 | 38 | multi := &Error{Errors: errors} 39 | if multi.Error() != expected { 40 | t.Fatalf("bad: %s", multi.Error()) 41 | } 42 | } 43 | 44 | func TestErrorErrorOrNil(t *testing.T) { 45 | var err *Error 46 | if err.ErrorOrNil() != nil { 47 | t.Fatalf("bad: %#v", err.ErrorOrNil()) 48 | } 49 | 50 | err = new(Error) 51 | if err.ErrorOrNil() != nil { 52 | t.Fatalf("bad: %#v", err.ErrorOrNil()) 53 | } 54 | 55 | err.Errors = []error{errors.New("foo")} 56 | if v := err.ErrorOrNil(); v == nil { 57 | t.Fatal("should not be nil") 58 | } else if !reflect.DeepEqual(v, err) { 59 | t.Fatalf("bad: %#v", v) 60 | } 61 | } 62 | 63 | func TestErrorWrappedErrors(t *testing.T) { 64 | errors := []error{ 65 | errors.New("foo"), 66 | errors.New("bar"), 67 | } 68 | 69 | multi := &Error{Errors: errors} 70 | if !reflect.DeepEqual(multi.Errors, multi.WrappedErrors()) { 71 | t.Fatalf("bad: %s", multi.WrappedErrors()) 72 | } 73 | 74 | multi = nil 75 | if err := multi.WrappedErrors(); err != nil { 76 | t.Fatalf("bad: %#v", multi) 77 | } 78 | } 79 | 80 | func TestErrorUnwrap(t *testing.T) { 81 | t.Run("with errors", func(t *testing.T) { 82 | err := &Error{Errors: []error{ 83 | errors.New("foo"), 84 | errors.New("bar"), 85 | errors.New("baz"), 86 | }} 87 | 88 | var current error = err 89 | for i := 0; i < len(err.Errors); i++ { 90 | current = errors.Unwrap(current) 91 | if !errors.Is(current, err.Errors[i]) { 92 | t.Fatal("should be next value") 93 | } 94 | } 95 | 96 | if errors.Unwrap(current) != nil { 97 | t.Fatal("should be nil at the end") 98 | } 99 | }) 100 | 101 | t.Run("with no errors", func(t *testing.T) { 102 | err := &Error{Errors: nil} 103 | if errors.Unwrap(err) != nil { 104 | t.Fatal("should be nil") 105 | } 106 | }) 107 | 108 | t.Run("with nil multierror", func(t *testing.T) { 109 | var err *Error 110 | if errors.Unwrap(err) != nil { 111 | t.Fatal("should be nil") 112 | } 113 | }) 114 | } 115 | 116 | func TestErrorIs(t *testing.T) { 117 | errBar := errors.New("bar") 118 | 119 | t.Run("with errBar", func(t *testing.T) { 120 | err := &Error{Errors: []error{ 121 | errors.New("foo"), 122 | errBar, 123 | errors.New("baz"), 124 | }} 125 | 126 | if !errors.Is(err, errBar) { 127 | t.Fatal("should be true") 128 | } 129 | }) 130 | 131 | t.Run("with errBar wrapped by fmt.Errorf", func(t *testing.T) { 132 | err := &Error{Errors: []error{ 133 | errors.New("foo"), 134 | fmt.Errorf("errorf: %w", errBar), 135 | errors.New("baz"), 136 | }} 137 | 138 | if !errors.Is(err, errBar) { 139 | t.Fatal("should be true") 140 | } 141 | }) 142 | 143 | t.Run("without errBar", func(t *testing.T) { 144 | err := &Error{Errors: []error{ 145 | errors.New("foo"), 146 | errors.New("baz"), 147 | }} 148 | 149 | if errors.Is(err, errBar) { 150 | t.Fatal("should be false") 151 | } 152 | }) 153 | } 154 | 155 | func TestErrorAs(t *testing.T) { 156 | match := &nestedError{} 157 | 158 | t.Run("with the value", func(t *testing.T) { 159 | err := &Error{Errors: []error{ 160 | errors.New("foo"), 161 | match, 162 | errors.New("baz"), 163 | }} 164 | 165 | var target *nestedError 166 | if !errors.As(err, &target) { 167 | t.Fatal("should be true") 168 | } 169 | if target == nil { 170 | t.Fatal("target should not be nil") 171 | } 172 | }) 173 | 174 | t.Run("with the value wrapped by fmt.Errorf", func(t *testing.T) { 175 | err := &Error{Errors: []error{ 176 | errors.New("foo"), 177 | fmt.Errorf("errorf: %w", match), 178 | errors.New("baz"), 179 | }} 180 | 181 | var target *nestedError 182 | if !errors.As(err, &target) { 183 | t.Fatal("should be true") 184 | } 185 | if target == nil { 186 | t.Fatal("target should not be nil") 187 | } 188 | }) 189 | 190 | t.Run("without the value", func(t *testing.T) { 191 | err := &Error{Errors: []error{ 192 | errors.New("foo"), 193 | errors.New("baz"), 194 | }} 195 | 196 | var target *nestedError 197 | if errors.As(err, &target) { 198 | t.Fatal("should be false") 199 | } 200 | if target != nil { 201 | t.Fatal("target should be nil") 202 | } 203 | }) 204 | } 205 | 206 | // nestedError implements error and is used for tests. 207 | type nestedError struct{} 208 | 209 | func (*nestedError) Error() string { return "" } 210 | 211 | func TestSetFormatter(t *testing.T) { 212 | var mErr *Error 213 | 214 | if want, have := (*Error)(nil), SetFormatter(mErr, ListFormatFunc); want != have { 215 | t.Errorf("bad: %#v", have) 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # multierror 2 | 3 | [![CircleCI](https://img.shields.io/circleci/build/github/hashicorp/go-multierror/master)](https://circleci.com/gh/hashicorp/go-multierror) 4 | [![Go Reference](https://pkg.go.dev/badge/github.com/hashicorp/go-multierror.svg)](https://pkg.go.dev/github.com/hashicorp/go-multierror) 5 | ![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/hashicorp/go-multierror) 6 | 7 | [circleci]: https://app.circleci.com/pipelines/github/hashicorp/go-multierror 8 | [godocs]: https://pkg.go.dev/github.com/hashicorp/go-multierror 9 | 10 | `multierror` is a package for Go that provides a mechanism for 11 | representing a list of `error` values as a single `error`. It is 12 | a fork off HashiCorp's [go-multierror](https://github.com/hashicorp/go-multierror) package. 13 | 14 | This allows a function in Go to return an `error` that might actually 15 | be a list of errors. If the caller knows this, they can unwrap the 16 | list and access the errors. If the caller doesn't know, the error 17 | formats to a nice human-readable format. 18 | 19 | `go-multierror` is fully compatible with the Go standard library 20 | [errors](https://golang.org/pkg/errors/) package, including the 21 | functions `As`, `Is`, and `Unwrap`. This provides a standardized approach 22 | for introspecting on error values. 23 | 24 | ## Installation and Docs 25 | 26 | Install using `go get github.com/tetratelabs/multierror`. 27 | 28 | Full documentation is available at 29 | https://pkg.go.dev/github.com/hashicorp/go-multierror 30 | 31 | ### Requires go version 1.13 or newer 32 | 33 | `go-multierror` requires go version 1.13 or newer. Go 1.13 introduced 34 | [error wrapping](https://golang.org/doc/go1.13#error_wrapping), which 35 | this library takes advantage of. 36 | 37 | If you need to use an earlier version of go, you can use the 38 | [v1.0.0](https://github.com/hashicorp/go-multierror/tree/v1.0.0) 39 | tag, which doesn't rely on features in go 1.13. 40 | 41 | If you see compile errors that look like the below, it's likely that 42 | you're on an older version of go: 43 | 44 | ``` 45 | /go/src/github.com/hashicorp/go-multierror/multierror.go:112:9: undefined: errors.As 46 | /go/src/github.com/hashicorp/go-multierror/multierror.go:117:9: undefined: errors.Is 47 | ``` 48 | 49 | ## Usage 50 | 51 | multierror is easy to use and purposely built to be unobtrusive in 52 | existing Go applications/libraries that may not be aware of it. 53 | 54 | **Building a list of errors** 55 | 56 | The `Append` function is used to create a list of errors. This function 57 | behaves a lot like the Go built-in `append` function: it doesn't matter 58 | if the first argument is nil, a `multierror.Error`, or any other `error`, 59 | the function behaves as you would expect. 60 | 61 | ```go 62 | var result error 63 | 64 | if err := step1(); err != nil { 65 | result = multierror.Append(result, err) 66 | } 67 | if err := step2(); err != nil { 68 | result = multierror.Append(result, err) 69 | } 70 | 71 | return result 72 | ``` 73 | 74 | **Customizing the formatting of the errors** 75 | 76 | By specifying a custom `ErrorFormat`, you can customize the format 77 | of the `Error() string` function: 78 | 79 | ```go 80 | var result *multierror.Error 81 | 82 | // ... accumulate errors here, maybe using Append 83 | 84 | if result != nil { 85 | result.ErrorFormat = func([]error) string { 86 | return "errors!" 87 | } 88 | } 89 | ``` 90 | 91 | **Accessing the list of errors** 92 | 93 | `multierror.Error` implements `error` so if the caller doesn't know about 94 | multierror, it will work just fine. But if you're aware a multierror might 95 | be returned, you can use type switches to access the list of errors: 96 | 97 | ```go 98 | if err := something(); err != nil { 99 | if merr, ok := err.(*multierror.Error); ok { 100 | // Use merr.Errors 101 | } 102 | } 103 | ``` 104 | 105 | You can also use the standard [`errors.Unwrap`](https://golang.org/pkg/errors/#Unwrap) 106 | function. This will continue to unwrap into subsequent errors until none exist. 107 | 108 | **Extracting an error** 109 | 110 | The standard library [`errors.As`](https://golang.org/pkg/errors/#As) 111 | function can be used directly with a multierror to extract a specific error: 112 | 113 | ```go 114 | // Assume err is a multierror value 115 | err := somefunc() 116 | 117 | // We want to know if "err" has a "RichErrorType" in it and extract it. 118 | var errRich RichErrorType 119 | if errors.As(err, &errRich) { 120 | // It has it, and now errRich is populated. 121 | } 122 | ``` 123 | 124 | **Checking for an exact error value** 125 | 126 | Some errors are returned as exact errors such as the [`ErrNotExist`](https://golang.org/pkg/os/#pkg-variables) 127 | error in the `os` package. You can check if this error is present by using 128 | the standard [`errors.Is`](https://golang.org/pkg/errors/#Is) function. 129 | 130 | ```go 131 | // Assume err is a multierror value 132 | err := somefunc() 133 | if errors.Is(err, os.ErrNotExist) { 134 | // err contains os.ErrNotExist 135 | } 136 | ``` 137 | 138 | **Returning a multierror only if there are errors** 139 | 140 | If you build a `multierror.Error`, you can use the `ErrorOrNil` function 141 | to return an `error` implementation only if there are errors to return: 142 | 143 | ```go 144 | var result *multierror.Error 145 | 146 | // ... accumulate errors here 147 | 148 | // Return the `error` only if errors were added to the multierror, otherwise 149 | // return nil since there are no errors. 150 | return result.ErrorOrNil() 151 | ``` 152 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | 3 | orbs: 4 | win: circleci/windows@2.2.0 5 | 6 | references: 7 | environment: &ENVIRONMENT 8 | TEST_RESULTS_PATH: &TEST_RESULTS_PATH /tmp/test-results 9 | WIN_TEST_RESULTS: &WIN_TEST_RESULTS c:\Users\circleci\AppData\Local\Temp\test-results 10 | 11 | commands: 12 | run-gotests: 13 | parameters: 14 | cmd: 15 | type: string 16 | platform: 17 | type: string 18 | steps: 19 | - run: 20 | name: "Run go tests" 21 | command: | 22 | PACKAGE_NAMES=$(go list ./... | circleci tests split --split-by=timings --timings-type=classname) 23 | echo "Running $(echo $PACKAGE_NAMES | wc -w) packages" 24 | echo $PACKAGE_NAMES 25 | << parameters.cmd >> --format=short-verbose --junitfile $TEST_RESULTS_PATH/go-multierror/gotestsum-report.xml -- -p 2 -cover -coverprofile=<< parameters.platform >>_cov_$CIRCLE_NODE_INDEX.part $PACKAGE_NAMES 26 | 27 | jobs: 28 | linux-tests: 29 | docker: 30 | - image: docker.mirror.hashicorp.services/circleci/golang:<< parameters.go-version >> 31 | parameters: 32 | go-version: 33 | type: string 34 | environment: 35 | <<: *ENVIRONMENT 36 | parallelism: 4 37 | steps: 38 | - run: go version 39 | - checkout 40 | - attach_workspace: 41 | at: . 42 | - run: mkdir -p $TEST_RESULTS_PATH/go-multierror 43 | 44 | # Restore go module cache if there is one 45 | - restore_cache: 46 | keys: 47 | - linux-gomod-cache-v1-{{ checksum "go.mod" }} 48 | 49 | - run: go mod download 50 | 51 | # Save go module cache if the go.mod file has changed 52 | - save_cache: 53 | key: linux-gomod-cache-v1-{{ checksum "go.mod" }} 54 | paths: 55 | - "/go/pkg/mod" 56 | 57 | # Check go fmt output because it does not report non-zero when there are fmt changes 58 | - run: 59 | name: check go fmt 60 | command: | 61 | files=$(go fmt ./...) 62 | if [ -n "$files" ]; then 63 | echo "The following file(s) do not conform to go fmt:" 64 | echo "$files" 65 | exit 1 66 | fi 67 | # Run go tests with gotestsum 68 | - run-gotests: 69 | cmd: "gotestsum" 70 | platform: "linux" 71 | 72 | # Save coverage report parts 73 | - persist_to_workspace: 74 | root: . 75 | paths: 76 | - linux_cov_*.part 77 | 78 | - store_test_results: 79 | path: *TEST_RESULTS_PATH 80 | - store_artifacts: 81 | path: *TEST_RESULTS_PATH 82 | 83 | windows-tests: 84 | executor: 85 | name: win/default 86 | shell: bash --login -eo pipefail 87 | environment: 88 | <<: *ENVIRONMENT 89 | working_directory: c:\gopath\src\github.com\hashicorp\go-multierror 90 | parameters: 91 | go-version: 92 | type: string 93 | gotestsum-version: 94 | type: string 95 | steps: 96 | - run: git config --global core.autocrlf false 97 | - checkout 98 | - attach_workspace: 99 | at: . 100 | - run: 101 | name: Setup (remove pre-installed go) 102 | command: | 103 | rm -rf "c:\Go" 104 | mkdir -p $TEST_RESULTS_PATH/go-multierror 105 | - restore_cache: 106 | keys: 107 | - win-golang-<< parameters.go-version >>-cache-v1 108 | - win-gomod-cache-{{ checksum "go.mod" }}-v1 109 | 110 | - run: 111 | name: Install go version << parameters.go-version >> 112 | command: | 113 | if [ ! -d "c:\go" ]; then 114 | echo "Cache not found, installing new version of go" 115 | curl --fail --location https://dl.google.com/go/go<< parameters.go-version >>.windows-amd64.zip --output go.zip 116 | unzip go.zip -d "/c" 117 | fi 118 | - run: 119 | command: go mod download 120 | 121 | - save_cache: 122 | key: win-golang-<< parameters.go-version >>-cache-v1 123 | paths: 124 | - /go 125 | 126 | - save_cache: 127 | key: win-gomod-cache-{{ checksum "go.mod" }}-v1 128 | paths: 129 | - c:\Windows\system32\config\systemprofile\go\pkg\mod 130 | 131 | - run: 132 | name: Install gotestsum 133 | command: | 134 | curl --fail --location https://github.com/gotestyourself/gotestsum/releases/download/v<< parameters.gotestsum-version >>/gotestsum_<< parameters.gotestsum-version >>_windows_amd64.tar.gz --output gotestsum.tar.gz 135 | tar -xvzf gotestsum.tar.gz 136 | - run-gotests: 137 | cmd: "./gotestsum.exe" 138 | platform: "win" 139 | 140 | # Save coverage report parts 141 | - persist_to_workspace: 142 | root: . 143 | paths: 144 | - win_cov_*.part 145 | 146 | - store_test_results: 147 | path: *WIN_TEST_RESULTS 148 | - store_artifacts: 149 | path: *WIN_TEST_RESULTS 150 | 151 | workflows: 152 | go-multierror: 153 | jobs: 154 | - linux-tests: 155 | matrix: 156 | parameters: 157 | go-version: ["1.13", "1.14", "1.15"] 158 | name: linux-test-go-<< matrix.go-version >> 159 | - windows-tests: 160 | matrix: 161 | parameters: 162 | go-version: ["1.13", "1.14", "1.15"] 163 | gotestsum-version: ["1.6.2"] 164 | name: win-test-go-<< matrix.go-version >> 165 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License, version 2.0 2 | 3 | 1. Definitions 4 | 5 | 1.1. “Contributor” 6 | 7 | means each individual or legal entity that creates, contributes to the 8 | creation of, or owns Covered Software. 9 | 10 | 1.2. “Contributor Version” 11 | 12 | means the combination of the Contributions of others (if any) used by a 13 | Contributor and that particular Contributor’s Contribution. 14 | 15 | 1.3. “Contribution” 16 | 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. “Covered Software” 20 | 21 | means Source Code Form to which the initial Contributor has attached the 22 | notice in Exhibit A, the Executable Form of such Source Code Form, and 23 | Modifications of such Source Code Form, in each case including portions 24 | thereof. 25 | 26 | 1.5. “Incompatible With Secondary Licenses” 27 | means 28 | 29 | a. that the initial Contributor has attached the notice described in 30 | Exhibit B to the Covered Software; or 31 | 32 | b. that the Covered Software was made available under the terms of version 33 | 1.1 or earlier of the License, but not also under the terms of a 34 | Secondary License. 35 | 36 | 1.6. “Executable Form” 37 | 38 | means any form of the work other than Source Code Form. 39 | 40 | 1.7. “Larger Work” 41 | 42 | means a work that combines Covered Software with other material, in a separate 43 | file or files, that is not Covered Software. 44 | 45 | 1.8. “License” 46 | 47 | means this document. 48 | 49 | 1.9. “Licensable” 50 | 51 | means having the right to grant, to the maximum extent possible, whether at the 52 | time of the initial grant or subsequently, any and all of the rights conveyed by 53 | this License. 54 | 55 | 1.10. “Modifications” 56 | 57 | means any of the following: 58 | 59 | a. any file in Source Code Form that results from an addition to, deletion 60 | from, or modification of the contents of Covered Software; or 61 | 62 | b. any new file in Source Code Form that contains any Covered Software. 63 | 64 | 1.11. “Patent Claims” of a Contributor 65 | 66 | means any patent claim(s), including without limitation, method, process, 67 | and apparatus claims, in any patent Licensable by such Contributor that 68 | would be infringed, but for the grant of the License, by the making, 69 | using, selling, offering for sale, having made, import, or transfer of 70 | either its Contributions or its Contributor Version. 71 | 72 | 1.12. “Secondary License” 73 | 74 | means either the GNU General Public License, Version 2.0, the GNU Lesser 75 | General Public License, Version 2.1, the GNU Affero General Public 76 | License, Version 3.0, or any later versions of those licenses. 77 | 78 | 1.13. “Source Code Form” 79 | 80 | means the form of the work preferred for making modifications. 81 | 82 | 1.14. “You” (or “Your”) 83 | 84 | means an individual or a legal entity exercising rights under this 85 | License. For legal entities, “You” includes any entity that controls, is 86 | controlled by, or is under common control with You. For purposes of this 87 | definition, “control” means (a) the power, direct or indirect, to cause 88 | the direction or management of such entity, whether by contract or 89 | otherwise, or (b) ownership of more than fifty percent (50%) of the 90 | outstanding shares or beneficial ownership of such entity. 91 | 92 | 93 | 2. License Grants and Conditions 94 | 95 | 2.1. Grants 96 | 97 | Each Contributor hereby grants You a world-wide, royalty-free, 98 | non-exclusive license: 99 | 100 | a. under intellectual property rights (other than patent or trademark) 101 | Licensable by such Contributor to use, reproduce, make available, 102 | modify, display, perform, distribute, and otherwise exploit its 103 | Contributions, either on an unmodified basis, with Modifications, or as 104 | part of a Larger Work; and 105 | 106 | b. under Patent Claims of such Contributor to make, use, sell, offer for 107 | sale, have made, import, and otherwise transfer either its Contributions 108 | or its Contributor Version. 109 | 110 | 2.2. Effective Date 111 | 112 | The licenses granted in Section 2.1 with respect to any Contribution become 113 | effective for each Contribution on the date the Contributor first distributes 114 | such Contribution. 115 | 116 | 2.3. Limitations on Grant Scope 117 | 118 | The licenses granted in this Section 2 are the only rights granted under this 119 | License. No additional rights or licenses will be implied from the distribution 120 | or licensing of Covered Software under this License. Notwithstanding Section 121 | 2.1(b) above, no patent license is granted by a Contributor: 122 | 123 | a. for any code that a Contributor has removed from Covered Software; or 124 | 125 | b. for infringements caused by: (i) Your and any other third party’s 126 | modifications of Covered Software, or (ii) the combination of its 127 | Contributions with other software (except as part of its Contributor 128 | Version); or 129 | 130 | c. under Patent Claims infringed by Covered Software in the absence of its 131 | Contributions. 132 | 133 | This License does not grant any rights in the trademarks, service marks, or 134 | logos of any Contributor (except as may be necessary to comply with the 135 | notice requirements in Section 3.4). 136 | 137 | 2.4. Subsequent Licenses 138 | 139 | No Contributor makes additional grants as a result of Your choice to 140 | distribute the Covered Software under a subsequent version of this License 141 | (see Section 10.2) or under the terms of a Secondary License (if permitted 142 | under the terms of Section 3.3). 143 | 144 | 2.5. Representation 145 | 146 | Each Contributor represents that the Contributor believes its Contributions 147 | are its original creation(s) or it has sufficient rights to grant the 148 | rights to its Contributions conveyed by this License. 149 | 150 | 2.6. Fair Use 151 | 152 | This License is not intended to limit any rights You have under applicable 153 | copyright doctrines of fair use, fair dealing, or other equivalents. 154 | 155 | 2.7. Conditions 156 | 157 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in 158 | Section 2.1. 159 | 160 | 161 | 3. Responsibilities 162 | 163 | 3.1. Distribution of Source Form 164 | 165 | All distribution of Covered Software in Source Code Form, including any 166 | Modifications that You create or to which You contribute, must be under the 167 | terms of this License. You must inform recipients that the Source Code Form 168 | of the Covered Software is governed by the terms of this License, and how 169 | they can obtain a copy of this License. You may not attempt to alter or 170 | restrict the recipients’ rights in the Source Code Form. 171 | 172 | 3.2. Distribution of Executable Form 173 | 174 | If You distribute Covered Software in Executable Form then: 175 | 176 | a. such Covered Software must also be made available in Source Code Form, 177 | as described in Section 3.1, and You must inform recipients of the 178 | Executable Form how they can obtain a copy of such Source Code Form by 179 | reasonable means in a timely manner, at a charge no more than the cost 180 | of distribution to the recipient; and 181 | 182 | b. You may distribute such Executable Form under the terms of this License, 183 | or sublicense it under different terms, provided that the license for 184 | the Executable Form does not attempt to limit or alter the recipients’ 185 | rights in the Source Code Form under this License. 186 | 187 | 3.3. Distribution of a Larger Work 188 | 189 | You may create and distribute a Larger Work under terms of Your choice, 190 | provided that You also comply with the requirements of this License for the 191 | Covered Software. If the Larger Work is a combination of Covered Software 192 | with a work governed by one or more Secondary Licenses, and the Covered 193 | Software is not Incompatible With Secondary Licenses, this License permits 194 | You to additionally distribute such Covered Software under the terms of 195 | such Secondary License(s), so that the recipient of the Larger Work may, at 196 | their option, further distribute the Covered Software under the terms of 197 | either this License or such Secondary License(s). 198 | 199 | 3.4. Notices 200 | 201 | You may not remove or alter the substance of any license notices (including 202 | copyright notices, patent notices, disclaimers of warranty, or limitations 203 | of liability) contained within the Source Code Form of the Covered 204 | Software, except that You may alter any license notices to the extent 205 | required to remedy known factual inaccuracies. 206 | 207 | 3.5. Application of Additional Terms 208 | 209 | You may choose to offer, and to charge a fee for, warranty, support, 210 | indemnity or liability obligations to one or more recipients of Covered 211 | Software. However, You may do so only on Your own behalf, and not on behalf 212 | of any Contributor. You must make it absolutely clear that any such 213 | warranty, support, indemnity, or liability obligation is offered by You 214 | alone, and You hereby agree to indemnify every Contributor for any 215 | liability incurred by such Contributor as a result of warranty, support, 216 | indemnity or liability terms You offer. You may include additional 217 | disclaimers of warranty and limitations of liability specific to any 218 | jurisdiction. 219 | 220 | 4. Inability to Comply Due to Statute or Regulation 221 | 222 | If it is impossible for You to comply with any of the terms of this License 223 | with respect to some or all of the Covered Software due to statute, judicial 224 | order, or regulation then You must: (a) comply with the terms of this License 225 | to the maximum extent possible; and (b) describe the limitations and the code 226 | they affect. Such description must be placed in a text file included with all 227 | distributions of the Covered Software under this License. Except to the 228 | extent prohibited by statute or regulation, such description must be 229 | sufficiently detailed for a recipient of ordinary skill to be able to 230 | understand it. 231 | 232 | 5. Termination 233 | 234 | 5.1. The rights granted under this License will terminate automatically if You 235 | fail to comply with any of its terms. However, if You become compliant, 236 | then the rights granted under this License from a particular Contributor 237 | are reinstated (a) provisionally, unless and until such Contributor 238 | explicitly and finally terminates Your grants, and (b) on an ongoing basis, 239 | if such Contributor fails to notify You of the non-compliance by some 240 | reasonable means prior to 60 days after You have come back into compliance. 241 | Moreover, Your grants from a particular Contributor are reinstated on an 242 | ongoing basis if such Contributor notifies You of the non-compliance by 243 | some reasonable means, this is the first time You have received notice of 244 | non-compliance with this License from such Contributor, and You become 245 | compliant prior to 30 days after Your receipt of the notice. 246 | 247 | 5.2. If You initiate litigation against any entity by asserting a patent 248 | infringement claim (excluding declaratory judgment actions, counter-claims, 249 | and cross-claims) alleging that a Contributor Version directly or 250 | indirectly infringes any patent, then the rights granted to You by any and 251 | all Contributors for the Covered Software under Section 2.1 of this License 252 | shall terminate. 253 | 254 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user 255 | license agreements (excluding distributors and resellers) which have been 256 | validly granted by You or Your distributors under this License prior to 257 | termination shall survive termination. 258 | 259 | 6. Disclaimer of Warranty 260 | 261 | Covered Software is provided under this License on an “as is” basis, without 262 | warranty of any kind, either expressed, implied, or statutory, including, 263 | without limitation, warranties that the Covered Software is free of defects, 264 | merchantable, fit for a particular purpose or non-infringing. The entire 265 | risk as to the quality and performance of the Covered Software is with You. 266 | Should any Covered Software prove defective in any respect, You (not any 267 | Contributor) assume the cost of any necessary servicing, repair, or 268 | correction. This disclaimer of warranty constitutes an essential part of this 269 | License. No use of any Covered Software is authorized under this License 270 | except under this disclaimer. 271 | 272 | 7. Limitation of Liability 273 | 274 | Under no circumstances and under no legal theory, whether tort (including 275 | negligence), contract, or otherwise, shall any Contributor, or anyone who 276 | distributes Covered Software as permitted above, be liable to You for any 277 | direct, indirect, special, incidental, or consequential damages of any 278 | character including, without limitation, damages for lost profits, loss of 279 | goodwill, work stoppage, computer failure or malfunction, or any and all 280 | other commercial damages or losses, even if such party shall have been 281 | informed of the possibility of such damages. This limitation of liability 282 | shall not apply to liability for death or personal injury resulting from such 283 | party’s negligence to the extent applicable law prohibits such limitation. 284 | Some jurisdictions do not allow the exclusion or limitation of incidental or 285 | consequential damages, so this exclusion and limitation may not apply to You. 286 | 287 | 8. Litigation 288 | 289 | Any litigation relating to this License may be brought only in the courts of 290 | a jurisdiction where the defendant maintains its principal place of business 291 | and such litigation shall be governed by laws of that jurisdiction, without 292 | reference to its conflict-of-law provisions. Nothing in this Section shall 293 | prevent a party’s ability to bring cross-claims or counter-claims. 294 | 295 | 9. Miscellaneous 296 | 297 | This License represents the complete agreement concerning the subject matter 298 | hereof. If any provision of this License is held to be unenforceable, such 299 | provision shall be reformed only to the extent necessary to make it 300 | enforceable. Any law or regulation which provides that the language of a 301 | contract shall be construed against the drafter shall not be used to construe 302 | this License against a Contributor. 303 | 304 | 305 | 10. Versions of the License 306 | 307 | 10.1. New Versions 308 | 309 | Mozilla Foundation is the license steward. Except as provided in Section 310 | 10.3, no one other than the license steward has the right to modify or 311 | publish new versions of this License. Each version will be given a 312 | distinguishing version number. 313 | 314 | 10.2. Effect of New Versions 315 | 316 | You may distribute the Covered Software under the terms of the version of 317 | the License under which You originally received the Covered Software, or 318 | under the terms of any subsequent version published by the license 319 | steward. 320 | 321 | 10.3. Modified Versions 322 | 323 | If you create software not governed by this License, and you want to 324 | create a new license for such software, you may create and use a modified 325 | version of this License if you rename the license and remove any 326 | references to the name of the license steward (except to note that such 327 | modified license differs from this License). 328 | 329 | 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses 330 | If You choose to distribute Source Code Form that is Incompatible With 331 | Secondary Licenses under the terms of this version of the License, the 332 | notice described in Exhibit B of this License must be attached. 333 | 334 | Exhibit A - Source Code Form License Notice 335 | 336 | This Source Code Form is subject to the 337 | terms of the Mozilla Public License, v. 338 | 2.0. If a copy of the MPL was not 339 | distributed with this file, You can 340 | obtain one at 341 | http://mozilla.org/MPL/2.0/. 342 | 343 | If it is not possible or desirable to put the notice in a particular file, then 344 | You may include the notice in a location (such as a LICENSE file in a relevant 345 | directory) where a recipient would be likely to look for such a notice. 346 | 347 | You may add additional accurate notices of copyright ownership. 348 | 349 | Exhibit B - “Incompatible With Secondary Licenses” Notice 350 | 351 | This Source Code Form is “Incompatible 352 | With Secondary Licenses”, as defined by 353 | the Mozilla Public License, v. 2.0. 354 | --------------------------------------------------------------------------------