diff --git a/errors/error.go b/errors/error.go index 89aca196ffc10e738e1f07839213cf1d29fde2ec..aee8f5d55c7ef86e5a427b9541f6aa35d3ba6941 100644 --- a/errors/error.go +++ b/errors/error.go @@ -23,28 +23,52 @@ type CustomError struct { // implement interface error: func (err *CustomError) Error() string { + if err == nil { + return "" + } + return err.Formatted(FormattingOptions{Causes: false}) } func (err *CustomError) BypassRaygun() *CustomError { + if err == nil { + return nil + } + err.bypassRaygun = true return err } func (err *CustomError) ShouldBypassRaygun() bool { + if err == nil { + return false + } + return err.bypassRaygun } func (err *CustomError) BypassSQSError() *CustomError { + if err == nil { + return nil + } + err.bypassSQSError = true return err } func (err *CustomError) ShouldBypassSQSError() bool { + if err == nil { + return false + } + return err.bypassSQSError } func Is(e1, e2 error) bool { + if e1 == nil || e2 == nil { + return false + } + if e1WithIs, ok := e1.(ErrorWithIs); ok { return e1WithIs.Is(e2) } @@ -53,6 +77,10 @@ func Is(e1, e2 error) bool { // Is() compares the message string of this or any cause to match the specified error message func (err *CustomError) Is(specificError error) bool { + if err == nil || specificError == nil { + return false + } + if err.message == specificError.Error() { return true } @@ -66,6 +94,10 @@ func (err *CustomError) Is(specificError error) bool { // implement github.com/pkg/errors: Cause func (err *CustomError) Cause() error { + if err == nil { + return nil + } + return err.cause } @@ -77,6 +109,10 @@ func HTTPCode(err error) int { } func (err *CustomError) Code() int { + if err == nil { + return 0 + } + // find http error code - returning the smallest code in the stack of causes (excluding code==0) code := err.code if err.cause != nil { @@ -91,6 +127,10 @@ func (err *CustomError) Code() int { } func (err *CustomError) Description() Description { + if err == nil { + return Description{} + } + info := err.caller.Info() desc := &Description{ Message: err.message, @@ -112,6 +152,10 @@ func (err *CustomError) Description() Description { } func (err *CustomError) Format(s fmt.State, v rune) { + if err == nil { + return + } + s.Write([]byte( err.Formatted( FormattingOptions{ @@ -130,6 +174,10 @@ type FormattingOptions struct { } func (err *CustomError) Formatted(opts FormattingOptions) string { + if err == nil { + return "" + } + // start with this error thisError := "" if opts.Source {