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

Fixes panics found in Juju for nil errors. #56

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,22 @@ type locationError struct {
}

// newLocationError constructs a new Locationer error from the supplied error
// with the location set to callDepth in the stack.
// with the location set to callDepth in the stack. If a nill error is provided
// to this function then a new empty error is constructed.
func newLocationError(err error, callDepth int) *locationError {
le := &locationError{error: err}
le.function, le.line = getLocation(callDepth + 1)
return le
}

// Error implementes the error interface.
func (l *locationError) Error() string {
if l.error == nil {
return ""
}
return l.error.Error()
}

// *locationError implements Locationer.Location interface
func (l *locationError) Location() (string, int) {
return l.function, l.line
Expand Down
6 changes: 3 additions & 3 deletions errortypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ func (e *errWithType) Unwrap() error {
}

func wrapErrorWithMsg(err error, msg string) error {
if msg == "" {
return err
}
if err == nil {
return stderror.New(msg)
}
if msg == "" {
return err
}
return fmt.Errorf("%s: %w", msg, err)
}

Expand Down
10 changes: 10 additions & 0 deletions errortypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,13 @@ func (*errorTypeSuite) TestAllErrors(c *gc.C) {

runErrorTests(c, errorTests, true)
}

// TestThatYouAlwaysGetError is a regression test for checking that the wrap
// constructor for our error types always returns a valid error object even if
// don't feed the construct with an instantiated error or a non empty string.
func (*errorTypeSuite) TestThatYouAlwaysGetError(c *gc.C) {
for _, errType := range allErrors {
err := errType.wrapConstructor(nil, "")
c.Assert(err.Error(), gc.Equals, "")
}
}