From 00481524c2b60fe2f97083874451635d726788b3 Mon Sep 17 00:00:00 2001
From: "daniel.naude" <danieln@bob.co.za>
Date: Wed, 19 Jun 2024 13:26:37 +0200
Subject: [PATCH] Add nil checks for error handling in CustomError methods

---
 errors/error.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/errors/error.go b/errors/error.go
index 89aca19..aee8f5d 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 {
-- 
GitLab