-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also update both server and client middlewares in thriftbp to support it.
- Loading branch information
Showing
15 changed files
with
302 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package errorsbp | ||
|
||
// Suppressor defines a type of function can be used to suppress certain errors. | ||
type Suppressor func(err error) bool | ||
|
||
// Suppress is the nil-safe way of calling a Suppressor. | ||
func (s Suppressor) Suppress(err error) bool { | ||
if s != nil { | ||
return s(err) | ||
} | ||
return SuppressNone(err) | ||
} | ||
|
||
// Wrap wraps the error based on the decision of Suppressor. | ||
// | ||
// If the error shall be suppressed, Wrap returns nil. | ||
// Otherwise Wrap returns the error as-is. | ||
func (s Suppressor) Wrap(err error) error { | ||
if s.Suppress(err) { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
// SuppressNone is a Suppressor implementation that always return false, | ||
// thus suppress none of the errors. | ||
// | ||
// It's the default implementation nil Suppressor falls back into. | ||
func SuppressNone(err error) bool { | ||
return false | ||
} | ||
|
||
var _ Suppressor = SuppressNone |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package errorsbp_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"math/rand" | ||
"reflect" | ||
"testing" | ||
"testing/quick" | ||
|
||
"github.com/reddit/baseplate.go/errorsbp" | ||
) | ||
|
||
func TestSuppressorNil(t *testing.T) { | ||
// Test nil safe that it doesn't panic. No real tests here. | ||
var s errorsbp.Suppressor | ||
s.Suppress(nil) | ||
s.Wrap(nil) | ||
} | ||
|
||
type specialError struct{} | ||
|
||
func (specialError) Error() string { | ||
return "special error" | ||
} | ||
|
||
func specialErrorSuppressor(err error) bool { | ||
return errors.As(err, new(specialError)) | ||
} | ||
|
||
type randomError struct { | ||
err error | ||
} | ||
|
||
func (randomError) Generate(r *rand.Rand, _ int) reflect.Value { | ||
var err error | ||
if r.Float64() < 0.2 { | ||
if r.Float64() < 0.5 { | ||
// For 10% (0.2*0.5) of chance, return specialError | ||
err = specialError{} | ||
} | ||
// For the rest 10%, return nil error | ||
} else { | ||
// For the rest 80%, use a random error | ||
err = fmt.Errorf("random error: %d", r.Int63()) | ||
} | ||
return reflect.ValueOf(randomError{ | ||
err: err, | ||
}) | ||
} | ||
|
||
var ( | ||
_ errorsbp.Suppressor = specialErrorSuppressor | ||
_ quick.Generator = randomError{} | ||
) | ||
|
||
func TestSuppressor(t *testing.T) { | ||
t.Run( | ||
"SuppressNone", | ||
func(t *testing.T) { | ||
var s errorsbp.Suppressor | ||
|
||
t.Run( | ||
"Suppress", | ||
func(t *testing.T) { | ||
f := func(e randomError) bool { | ||
return !s.Suppress(e.err) | ||
} | ||
if err := quick.Check(f, nil); err != nil { | ||
t.Error(err) | ||
} | ||
}, | ||
) | ||
|
||
t.Run( | ||
"Wrap", | ||
func(t *testing.T) { | ||
f := func(e randomError) bool { | ||
wrapped := s.Wrap(e.err) | ||
if wrapped != e.err { | ||
t.Errorf("Expected unchanged error %v, got %v", e.err, wrapped) | ||
} | ||
return !t.Failed() | ||
} | ||
if err := quick.Check(f, nil); err != nil { | ||
t.Error(err) | ||
} | ||
}, | ||
) | ||
}, | ||
) | ||
|
||
t.Run( | ||
"specialErrorSuppressor", | ||
func(t *testing.T) { | ||
var s errorsbp.Suppressor = specialErrorSuppressor | ||
|
||
t.Run( | ||
"Suppress", | ||
func(t *testing.T) { | ||
f := func(e randomError) bool { | ||
err := e.err | ||
expected := errors.As(err, new(specialError)) | ||
actual := s.Suppress(err) | ||
if actual != expected { | ||
t.Errorf("Expected %v for err %v, got %v", expected, err, actual) | ||
} | ||
return !t.Failed() | ||
} | ||
if err := quick.Check(f, nil); err != nil { | ||
t.Error(err) | ||
} | ||
}, | ||
) | ||
|
||
t.Run( | ||
"Wrap", | ||
func(t *testing.T) { | ||
f := func(e randomError) bool { | ||
err := e.err | ||
expected := err | ||
if errors.As(err, new(specialError)) { | ||
expected = nil | ||
} | ||
actual := s.Wrap(err) | ||
if actual != expected { | ||
t.Errorf("Expected %v for error %v, got %v", expected, err, actual) | ||
} | ||
return !t.Failed() | ||
} | ||
if err := quick.Check(f, nil); err != nil { | ||
t.Error(err) | ||
} | ||
}, | ||
) | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.