From ad06035d6e83cc827c1606c468c7f708f9bf7d88 Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Sat, 14 Dec 2024 20:12:06 +0700 Subject: [PATCH] :recycle: refactor: refactor codebase #2 --- init.go | 4 +- test/wraperr_test.go | 119 +++++++++++++++++++++++++++++++++++++++++++ wraperr.go | 20 ++++---- 3 files changed, 131 insertions(+), 12 deletions(-) diff --git a/init.go b/init.go index 12da5cc..12b10a5 100644 --- a/init.go +++ b/init.go @@ -679,7 +679,7 @@ func (w *wrapper) WithErrSck(err error) *wrapper { // Returns: // - A pointer to the modified `wrapper` instance to support method chaining. func (w *wrapper) WithErrWrap(err error, message string) *wrapper { - w.errors = Wrap(err, message) + w.errors = WithErrWrap(err, message) return w } @@ -696,7 +696,7 @@ func (w *wrapper) WithErrWrap(err error, message string) *wrapper { // Returns: // - A pointer to the modified `wrapper` instance to support method chaining. func (w *wrapper) WithErrWrapf(err error, format string, args ...interface{}) *wrapper { - w.errors = Wrapf(err, format, args...) + w.errors = WithErrWrapf(err, format, args...) return w } diff --git a/test/wraperr_test.go b/test/wraperr_test.go index 56e5404..26908e6 100644 --- a/test/wraperr_test.go +++ b/test/wraperr_test.go @@ -1 +1,120 @@ package test + +import ( + "errors" + "fmt" + "testing" + + "github.com/sivaosorg/wrapify" +) + +func TestWithError(t *testing.T) { + err := wrapify.WithError("Test error") + if err == nil { + t.Errorf("Expected error, got nil") + } + if err.Error() != "Test error" { + t.Errorf("Expected 'Test error', got %s", err.Error()) + } +} + +func TestWithErrorf(t *testing.T) { + err := wrapify.WithErrorf("Failed to load file %s", "test.txt") + if err == nil { + t.Errorf("Expected error, got nil") + } + if err.Error() != "Failed to load file test.txt" { + t.Errorf("Expected 'Failed to load file test.txt', got %s", err.Error()) + } +} + +func TestWithErrStack(t *testing.T) { + originalErr := errors.New("original error") + errWithStack := wrapify.WithErrStack(originalErr) + if errWithStack == nil { + t.Errorf("Expected error with stack, got nil") + } + if errWithStack.Error() != "original error" { + t.Errorf("Expected 'original error', got %s", errWithStack.Error()) + } +} + +func TestWrap(t *testing.T) { + originalErr := errors.New("file not found") + wrappedErr := wrapify.WithErrWrap(originalErr, "Failed to read the file") + if wrappedErr == nil { + t.Errorf("Expected wrapped error, got nil") + } + if wrappedErr.Error() != "Failed to read the file: file not found" { + t.Errorf("Expected 'Failed to read the file: file not found', got %s", wrappedErr.Error()) + } +} + +func TestWrapf(t *testing.T) { + originalErr := errors.New("file not found") + wrappedErr := wrapify.WithErrWrapf(originalErr, "Failed to open file %s", "data.txt") + if wrappedErr == nil { + t.Errorf("Expected wrapped error, got nil") + } + if wrappedErr.Error() != "Failed to open file data.txt: file not found" { + t.Errorf("Expected 'Failed to open file data.txt: file not found', got %s", wrappedErr.Error()) + } +} + +func TestWithMessage(t *testing.T) { + originalErr := errors.New("original error") + errWithMessage := wrapify.WithMessage(originalErr, "Additional context") + if errWithMessage == nil { + t.Errorf("Expected error with message, got nil") + } + if errWithMessage.Error() != "Additional context: original error" { + t.Errorf("Expected 'Additional context: original error', got %s", errWithMessage.Error()) + } +} + +func TestWithMessagef(t *testing.T) { + originalErr := errors.New("original error") + errWithMessagef := wrapify.WithMessagef(originalErr, "Context: %s", "something went wrong") + if errWithMessagef == nil { + t.Errorf("Expected error with message, got nil") + } + if errWithMessagef.Error() != "Context: something went wrong: original error" { + t.Errorf("Expected 'Context: something went wrong: original error', got %s", errWithMessagef.Error()) + } +} + +func TestCause(t *testing.T) { + originalErr := errors.New("file not found") + wrappedErr := wrapify.WithErrWrap(originalErr, "Failed to open file") + causeErr := wrapify.Cause(wrappedErr) + if causeErr == nil { + t.Errorf("Expected cause error, got nil") + } + if causeErr.Error() != "file not found" { + t.Errorf("Expected 'file not found', got %s", causeErr.Error()) + } +} + +func TestErrorFormatting(t *testing.T) { + err := wrapify.WithError("Test error") + if err.Error() != "Test error" { + t.Errorf("Expected 'Test error', got %s", err.Error()) + } + + // Ensure that the format includes the stack trace if the '+' flag is set + if errFmt := fmt.Sprintf("%+v", err); errFmt == "Test error" { + t.Errorf("Expected formatted string with stack trace, got %s", errFmt) + } +} + +// func TestUnwrap(t *testing.T) { +// originalErr := errors.New("file not found") +// wrappedErr := wrapify.Wrap(originalErr, "Failed to open file") +// unwrappedErr := wrappedErr.(*underlyingStack).Unwrap() +// if unwrappedErr == nil { +// t.Errorf("Expected unwrapped error, got nil") +// } +// if unwrappedErr.Error() != "file not found" { +// t.Errorf("Expected 'file not found', got %s", unwrappedErr.Error()) +// } +// } diff --git a/wraperr.go b/wraperr.go index 21c7949..e1ad881 100644 --- a/wraperr.go +++ b/wraperr.go @@ -53,16 +53,16 @@ func WithErrStack(err error) error { } } -// Wrap returns an error that annotates the provided error with a new message -// and a stack trace at the point Wrap was called. If the provided error is nil, -// Wrap returns nil. +// WithErrWrap returns an error that annotates the provided error with a new message +// and a stack trace at the point WithErrWrap was called. If the provided error is nil, +// WithErrWrap returns nil. // // Usage example: // // err := errors.New("file not found") -// wrappedErr := Wrap(err, "Failed to read the file") +// wrappedErr := WithErrWrap(err, "Failed to read the file") // fmt.Println(wrappedErr) // "Failed to read the file: file not found" with stack trace -func Wrap(err error, message string) error { +func WithErrWrap(err error, message string) error { if err == nil { return nil } @@ -76,16 +76,16 @@ func Wrap(err error, message string) error { } } -// Wrapf returns an error that annotates the provided error with a formatted message -// and a stack trace at the point Wrapf was called. If the provided error is nil, -// Wrapf returns nil. +// WithErrWrapf returns an error that annotates the provided error with a formatted message +// and a stack trace at the point WithErrWrapf was called. If the provided error is nil, +// WithErrWrapf returns nil. // // Usage example: // // err := errors.New("file not found") -// wrappedErr := Wrapf(err, "Failed to load file %s", filename) +// wrappedErr := WithErrWrapf(err, "Failed to load file %s", filename) // fmt.Println(wrappedErr) // "Failed to load file : file not found" with stack trace -func Wrapf(err error, format string, args ...interface{}) error { +func WithErrWrapf(err error, format string, args ...interface{}) error { if err == nil { return nil }