-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error messages in ygot.Merge. (#815)
* Improve error messages in ygot.Merge. Fail slow: gather error messages about conflicting fields prior to returning to user. Also print the access path to the user so they know which fields contain the conflict. * Import 1.20 errors.Join code to maintain compatibility with 1.18 * Update ygot/join.go Co-authored-by: Likai Liu <[email protected]> * improve style --------- Co-authored-by: Likai Liu <[email protected]>
- Loading branch information
Showing
4 changed files
with
249 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2023 Google Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Adapted from errors.Join() introduced in go1.20 for compatibility with older Go versions. | ||
|
||
// Copyright 2022 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package ygot | ||
|
||
// joinErrors returns an error that wraps the given errors. | ||
// Any nil error values are discarded. | ||
// joinErrors returns nil if errs contains no non-nil values. | ||
// The error formats as the concatenation of the strings obtained | ||
// by calling the Error method of each element of errs, with a newline | ||
// between each string. | ||
func joinErrors(errs ...error) error { | ||
n := 0 | ||
for _, err := range errs { | ||
if err != nil { | ||
n++ | ||
} | ||
} | ||
if n == 0 { | ||
return nil | ||
} | ||
e := &joinError{ | ||
errs: make([]error, 0, n), | ||
} | ||
for _, err := range errs { | ||
if err != nil { | ||
e.errs = append(e.errs, err) | ||
} | ||
} | ||
return e | ||
} | ||
|
||
type joinError struct { | ||
errs []error | ||
} | ||
|
||
func (e *joinError) Error() string { | ||
var b []byte | ||
for i, err := range e.errs { | ||
if i > 0 { | ||
b = append(b, '\n') | ||
} | ||
b = append(b, err.Error()...) | ||
} | ||
return string(b) | ||
} | ||
|
||
func (e *joinError) Unwrap() []error { | ||
return e.errs | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2023 Google Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Adapted from https://cs.opensource.google/go/go/+/refs/tags/go1.20.3:src/errors/join.go | ||
|
||
// Copyright 2022 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package ygot | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestJoinReturnsNil(t *testing.T) { | ||
if err := joinErrors(); err != nil { | ||
t.Errorf("joinErrors() = %v, want nil", err) | ||
} | ||
if err := joinErrors(nil); err != nil { | ||
t.Errorf("joinErrors(nil) = %v, want nil", err) | ||
} | ||
if err := joinErrors(nil, nil); err != nil { | ||
t.Errorf("joinErrors(nil, nil) = %v, want nil", err) | ||
} | ||
} | ||
|
||
func TestJoin(t *testing.T) { | ||
err1 := errors.New("err1") | ||
err2 := errors.New("err2") | ||
for _, test := range []struct { | ||
errs []error | ||
want []error | ||
}{{ | ||
errs: []error{err1}, | ||
want: []error{err1}, | ||
}, { | ||
errs: []error{err1, err2}, | ||
want: []error{err1, err2}, | ||
}, { | ||
errs: []error{err1, nil, err2}, | ||
want: []error{err1, err2}, | ||
}} { | ||
got := joinErrors(test.errs...).(interface{ Unwrap() []error }).Unwrap() | ||
if !reflect.DeepEqual(got, test.want) { | ||
t.Errorf("Join(%v) = %v; want %v", test.errs, got, test.want) | ||
} | ||
if len(got) != cap(got) { | ||
t.Errorf("Join(%v) returns errors with len=%v, cap=%v; want len==cap", test.errs, len(got), cap(got)) | ||
} | ||
} | ||
} | ||
|
||
func TestJoinErrorMethod(t *testing.T) { | ||
err1 := errors.New("err1") | ||
err2 := errors.New("err2") | ||
for _, test := range []struct { | ||
errs []error | ||
want string | ||
}{{ | ||
errs: []error{err1}, | ||
want: "err1", | ||
}, { | ||
errs: []error{err1, err2}, | ||
want: "err1\nerr2", | ||
}, { | ||
errs: []error{err1, nil, err2}, | ||
want: "err1\nerr2", | ||
}} { | ||
got := joinErrors(test.errs...).Error() | ||
if got != test.want { | ||
t.Errorf("Join(%v).Error() = %q; want %q", test.errs, got, test.want) | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.