Skip to content

Commit

Permalink
♻️ refactor: refactor codebase #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Dec 14, 2024
1 parent 733339e commit ad06035
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 12 deletions.
4 changes: 2 additions & 2 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
119 changes: 119 additions & 0 deletions test/wraperr_test.go
Original file line number Diff line number Diff line change
@@ -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())
// }
// }
20 changes: 10 additions & 10 deletions wraperr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 <filename>: 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
}
Expand Down

0 comments on commit ad06035

Please sign in to comment.