From d7a4bf734105891a05731bc5a06049645c6b63e3 Mon Sep 17 00:00:00 2001 From: Jan Semmelink <jan@uafrica.com> Date: Wed, 8 Sep 2021 14:40:56 +0200 Subject: [PATCH] Renamed Error to CustomError and created Error() func to be similar to Wrap() without formatting --- errors/error.go | 21 ++++++++++----------- errors/error_formats_test.go | 4 ++-- errors/errors.go | 21 +++++++++------------ errors/errors_test.go | 2 +- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/errors/error.go b/errors/error.go index 38a9fad..f78c694 100644 --- a/errors/error.go +++ b/errors/error.go @@ -7,34 +7,33 @@ import ( "strings" ) -//Error implements the following interfaces: +//CustomError implements the following interfaces: // error // github.com/pkg/errors: Cause -type Error struct { +type CustomError struct { message string caller Caller - //stack []CallerInfo - cause error + cause error } //implement interface error: -func (err Error) Error() string { +func (err CustomError) Error() string { return err.Formatted(FormattingOptions{Causes: true}) } //implement github.com/pkg/errors: Cause -func (err Error) Cause() error { +func (err CustomError) Cause() error { return err.cause } -func (err Error) Description() Description { +func (err CustomError) Description() Description { info := err.caller.Info() desc := &Description{ Message: err.message, Source: &info, } if err.cause != nil { - causeWithStack, ok := err.cause.(*Error) + causeWithStack, ok := err.cause.(*CustomError) if !ok { //external cause without our stack //if github.com/pkg/errors, we can still get caller reference @@ -48,7 +47,7 @@ func (err Error) Description() Description { return *desc } -func (err Error) Format(s fmt.State, v rune) { +func (err CustomError) Format(s fmt.State, v rune) { s.Write([]byte( err.Formatted( FormattingOptions{ @@ -66,7 +65,7 @@ type FormattingOptions struct { Source bool } -func (err Error) Formatted(opts FormattingOptions) string { +func (err CustomError) Formatted(opts FormattingOptions) string { //start with this error thisError := "" if opts.Source { @@ -93,7 +92,7 @@ func (err Error) Formatted(opts FormattingOptions) string { sep += " " } - if causeWithStack, ok := err.cause.(*Error); ok { + if causeWithStack, ok := err.cause.(*CustomError); ok { return thisError + sep + causeWithStack.Formatted(opts) } diff --git a/errors/error_formats_test.go b/errors/error_formats_test.go index 46ca107..bd11992 100644 --- a/errors/error_formats_test.go +++ b/errors/error_formats_test.go @@ -265,13 +265,13 @@ func show(t *testing.T, title string, err error) { t.Logf("------------------------------------------------------------------------------------") t.Logf("err.Error(): %s", err.Error()) t.Logf("Printf(%%s): %s", err) - if _, ok := err.(*uf_errors.Error); ok { //compact only supported on our own lib: + if _, ok := err.(*uf_errors.CustomError); ok { //compact only supported on our own lib: t.Logf("Printf(%%c): %c", err) t.Logf("Printf(%%+c): %+c", err) } t.Logf("Printf(%%v): %v", err) t.Logf("Printf(%%+v): %+v", err) - if e, ok := err.(*uf_errors.Error); ok { //description only supported on our own lib: + if e, ok := err.(*uf_errors.CustomError); ok { //description only supported on our own lib: desc := e.Description() jsonDesc, _ := json.Marshal(desc) t.Logf("JSON: %s", string(jsonDesc)) diff --git a/errors/errors.go b/errors/errors.go index ca18e57..0288721 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -13,31 +13,28 @@ type ErrorWithCause interface { } func New(message string) error { - err := &Error{ + err := &CustomError{ message: message, caller: GetCaller(2), - //stack: Stack(3), - cause: nil, + cause: nil, } return err } -func NError(message string) error { - err := &Error{ +func Error(message string) error { + err := &CustomError{ message: message, caller: GetCaller(2), - //stack: Stack(3), - cause: nil, + cause: nil, } return err } func Errorf(format string, args ...interface{}) error { - err := &Error{ + err := &CustomError{ message: fmt.Sprintf(format, args...), caller: GetCaller(2), - //stack: Stack(3), - cause: nil, + cause: nil, } return err } @@ -47,7 +44,7 @@ func Wrapf(err error, format string, args ...interface{}) error { return nil } - wrappedErr := &Error{ + wrappedErr := &CustomError{ message: fmt.Sprintf(format, args...), caller: GetCaller(2), cause: err, @@ -61,7 +58,7 @@ func Wrap(err error, msg string) error { return nil } - wrappedErr := &Error{ + wrappedErr := &CustomError{ message: msg, caller: GetCaller(2), cause: err, diff --git a/errors/errors_test.go b/errors/errors_test.go index 0c40721..cd039f2 100644 --- a/errors/errors_test.go +++ b/errors/errors_test.go @@ -14,7 +14,7 @@ func TestErrorFormatting(t *testing.T) { e1 := errors.Errorf("you have problem in your SQL near xxx") e2 := errors.Wrapf(e1, "query failed") e3 := errors.Wrapf(e2, "failed to find account") - e4 := errors.Wrapf(e3, "login failed").(*errors.Error) + e4 := errors.Wrapf(e3, "login failed").(*errors.CustomError) //we can log the error in different ways: -- GitLab