Skip to content
forked from h-fam/errdiff

Type-safe errdiff that can check wrapped errors ⭐

License

Notifications You must be signed in to change notification settings

mrwormhole/errdiff

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Errdiff

Version CI Build GoDoc Report Card License Coverage Status

This is a fork of h-fam/errdiff, this is created in order to achieve type-safety and better Check() method that can understand wrapped/contained errors. In the process of doing so, I have removed interface{} argument that is passed to Check() method. Also cleaned up deprecated grpc status code pkgs and made diffs more consistent.

Moral

  • Avoid using interface{} or any for test tables which creates havoc on test readability and maintenance. Use statically typed test methods with statically typed table tests to reduce the mental burden.
  • Consistent error diff reporting with = and : and , distinguished.
  • Single purpose for a single function, no generic test funcs

Usage

errdiff.Check

The most common option that is used to compare against error

tests := []struct {
  ...
  wantErr error
}{
  // Success
  {...},
  // Failures
  {..., wantErr: errors.New("something failed: EOF")}, // an explicit full error
  {..., wantErr: io.EOF}, // a contained/wrapped error
}
for _, tt := range tests {
  t.Run(tt.name, func(t *testing.T) {
    got, err := fn(...)
    if diff := errdiff.Check(err, tt.wantErr); diff != "" {
      t.Errorf("fn() %s", diff)
    }
  })
}

errdiff.Text

It is used for exact strings to compare against error

tests := []struct {
  ...
  wantErr string
}{
  // Success
  {...},
  // Failures
  {..., wantErr: "something failed: EOF"}, // full text case-sensitive
}
for _, tt := range tests {
  t.Run(tt.name, func(t *testing.T) {
    got, err := fn(...)
    if diff := errdiff.Text(err, tt.wantErr); diff != "" {
      t.Errorf("fn() %s", diff)
    }
  })
}

errdiff.Code

It is used for grpc status codes to compare against error

tests := []struct {
  ...
  wantCode codes.Code
}{
  // Success
  {...},
  // Failures
  {..., wantCode: codes.InvalidArgument}, // grpc status code
}
for _, tt := range tests {
  t.Run(tt.name, func(t *testing.T) {
    got, err := fn(...)
    if diff := errdiff.Code(err, tt.wantCode); diff != "" {
      t.Errorf("fn() %s", diff)
    }
  })
}

About

Type-safe errdiff that can check wrapped errors ⭐

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%