From a82095e18fc28d3339bac6a6c2197e8e78ef1f2c Mon Sep 17 00:00:00 2001 From: jano3 <jano@bob.co.za> Date: Thu, 15 Feb 2024 10:00:12 +0200 Subject: [PATCH] Expand custom error with bypassRaygun and bypassSQSError --- errors/error.go | 42 +++++++++++++++++++++++++++++++----------- errors/errors.go | 16 ++++++++-------- logs/logs.go | 4 ++++ 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/errors/error.go b/errors/error.go index d20acd5..e512b57 100644 --- a/errors/error.go +++ b/errors/error.go @@ -13,17 +13,37 @@ import ( // error // github.com/pkg/errors: Cause type CustomError struct { - code int - message string - caller Caller - cause error + code int + message string + caller Caller + cause error + bypassRaygun bool + bypassSQSError bool } // implement interface error: -func (err CustomError) Error() string { +func (err *CustomError) Error() string { return err.Formatted(FormattingOptions{Causes: false}) } +func (err *CustomError) BypassRaygun() error { + err.bypassRaygun = true + return err +} + +func (err *CustomError) ShouldBypassRaygun() bool { + return err.bypassRaygun +} + +func (err *CustomError) BypassSQSError() error { + err.bypassSQSError = true + return err +} + +func (err *CustomError) ShouldSQSError() bool { + return err.bypassSQSError +} + func Is(e1, e2 error) bool { if e1WithIs, ok := e1.(ErrorWithIs); ok { return e1WithIs.Is(e2) @@ -32,7 +52,7 @@ 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 { +func (err *CustomError) Is(specificError error) bool { if err.message == specificError.Error() { return true } @@ -45,7 +65,7 @@ func (err CustomError) Is(specificError error) bool { } // implement github.com/pkg/errors: Cause -func (err CustomError) Cause() error { +func (err *CustomError) Cause() error { return err.cause } @@ -56,7 +76,7 @@ func HTTPCode(err error) int { return 0 } -func (err CustomError) Code() int { +func (err *CustomError) Code() int { // find http error code - returning the smallest code in the stack of causes (excluding code==0) code := err.code if err.cause != nil { @@ -70,7 +90,7 @@ func (err CustomError) Code() int { return code } -func (err CustomError) Description() Description { +func (err *CustomError) Description() Description { info := err.caller.Info() desc := &Description{ Message: err.message, @@ -91,7 +111,7 @@ func (err CustomError) Description() Description { return *desc } -func (err CustomError) Format(s fmt.State, v rune) { +func (err *CustomError) Format(s fmt.State, v rune) { s.Write([]byte( err.Formatted( FormattingOptions{ @@ -109,7 +129,7 @@ type FormattingOptions struct { Source bool } -func (err CustomError) Formatted(opts FormattingOptions) string { +func (err *CustomError) Formatted(opts FormattingOptions) string { // start with this error thisError := "" if opts.Source { diff --git a/errors/errors.go b/errors/errors.go index d8245cc..e0ead60 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -18,7 +18,7 @@ type ErrorWithIs interface { Is(specificError error) bool } -func Error(message string) error { +func Error(message string) *CustomError { err := &CustomError{ message: message, caller: GetCaller(2), @@ -27,7 +27,7 @@ func Error(message string) error { return err } -func Errorf(format string, args ...interface{}) error { +func Errorf(format string, args ...interface{}) *CustomError { err := &CustomError{ message: fmt.Sprintf(format, args...), caller: GetCaller(2), @@ -36,7 +36,7 @@ func Errorf(format string, args ...interface{}) error { return err } -func Wrapf(err error, format string, args ...interface{}) error { +func Wrapf(err error, format string, args ...interface{}) *CustomError { if err == nil { return nil } @@ -50,7 +50,7 @@ func Wrapf(err error, format string, args ...interface{}) error { return wrappedErr } -func Wrap(err error, msg string) error { +func Wrap(err error, msg string) *CustomError { if err == nil { return nil } @@ -64,7 +64,7 @@ func Wrap(err error, msg string) error { return wrappedErr } -func HTTP(code int, err error, format string, args ...interface{}) error { +func HTTP(code int, err error, format string, args ...interface{}) *CustomError { wrappedErr := &CustomError{ code: code, message: fmt.Sprintf(format, args...), @@ -75,7 +75,7 @@ func HTTP(code int, err error, format string, args ...interface{}) error { return wrappedErr } -func HTTPWithMsg(code int, format string, args ...interface{}) error { +func HTTPWithMsg(code int, format string, args ...interface{}) *CustomError { errorString := fmt.Sprintf(format, args...) wrappedErr := &CustomError{ @@ -88,11 +88,11 @@ func HTTPWithMsg(code int, format string, args ...interface{}) error { return wrappedErr } -func HTTPCodeOnly(code int) error { +func HTTPCodeOnly(code int) *CustomError { return HTTP(code, nil, "") } -func HTTPWithError(code int, err error) error { +func HTTPWithError(code int, err error) *CustomError { var errorMessage string // This check is here just as a failsafe to seg faults, if err is nil then just return assign an empty string as message. if err != nil { diff --git a/logs/logs.go b/logs/logs.go index b1f8ef6..f55fa6f 100644 --- a/logs/logs.go +++ b/logs/logs.go @@ -417,6 +417,10 @@ func sendRaygunError(fields map[string]interface{}, errToSend error) { return } + if customErr, ok := errToSend.(*errors.CustomError); ok && customErr.ShouldBypassRaygun() { + return + } + env := getEnvironment() tags := []string{env} -- GitLab