-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(perp): Remove unnecessary panics | #1 #1126
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e00712c
remove unnecessary panics in x/perp
Unique-Divine 5bed36e
change log
Unique-Divine bdb30af
chore: re-run linter
Unique-Divine 5033e7b
test(oracle): stop the tyrannical behavior of tally_fuzz_test.go
Unique-Divine 699257f
test(oracle): try assert.NotPanics for keys of length 0
Unique-Divine 3a4c7c1
feat,test(common): Create StringSet for easy set management
Unique-Divine ad7e866
test(oracle): Increase coverage and end the collections panics in
Unique-Divine fd4be81
fix(oracle): small bug in TestFuzz_PickReferencePair
Unique-Divine 1640306
test,feat(common): address PR feedback and add a more generic version…
Unique-Divine 74cbfd3
linter
Unique-Divine 9794d2a
Update x/common/error_test.go
Unique-Divine 865ffc9
Merge branch 'master' into realu/remove-panics-perp
Unique-Divine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,182 @@ | ||
package common | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"runtime/debug" | ||
) | ||
|
||
// TryCatch is an implementation of the try-catch block from languages like C++ and JS. | ||
// Given a 'callback' function, TryCatch defers and recovers from any panics or | ||
// errors, allowing one to safely handle multiple panics in succession. | ||
// | ||
// Typically, you'll write something like: `err := TryCatch(aRiskyFunction)()` | ||
// | ||
// Usage example: | ||
// | ||
// var calmPanic error = TryCatch(func() { | ||
// panic("something crazy") | ||
// })() | ||
// fmt.Println(calmPanic.Error()) // prints "something crazy" | ||
// | ||
// Note that TryCatch doesn't return an error. It returns a function that returns | ||
// an error. Only when you call the output of TryCatch will it "feel" like a | ||
// try-catch block from other languages. | ||
// | ||
// This means that TryCatch can be used to restart go routines that panic as well. | ||
func TryCatch(callback func()) func() error { | ||
return func() (err error) { | ||
defer func() { | ||
if panicInfo := recover(); panicInfo != nil { | ||
err = fmt.Errorf("%v, %s", panicInfo, string(debug.Stack())) | ||
return | ||
} | ||
}() | ||
callback() // calling the decorated function | ||
return err | ||
} | ||
} | ||
|
||
// ToError converts a value to an error type if it: | ||
// (1) is a string, | ||
// (2) has a String() function | ||
// (3) is already an error. | ||
// (4) or is a slice of these cases | ||
// I.e., the types supported are: | ||
// string, []string, error, []error, fmt.Stringer, []fmt.Stringer | ||
// | ||
// The type is inferred from try catch blocks at runtime. | ||
func ToError(v any) (out error, ok bool) { | ||
if v == nil { | ||
return nil, true | ||
} | ||
switch v := v.(type) { | ||
case string: | ||
return errors.New(v), true | ||
case error: | ||
return v, true | ||
case fmt.Stringer: | ||
return errors.New(v.String()), true | ||
case []string: | ||
return toErrorFromSlice(v) | ||
case []error: | ||
return toErrorFromSlice(v) | ||
case []fmt.Stringer: | ||
return toErrorFromSlice(v) | ||
default: | ||
// Cases for duck typing at runtime | ||
|
||
// case: error | ||
if tcErr := TryCatch(func() { | ||
v := v.(error) | ||
out = errors.New(v.Error()) | ||
})(); tcErr == nil { | ||
return out, true | ||
} | ||
|
||
// case: string | ||
if tcErr := TryCatch(func() { | ||
v := v.(string) | ||
out = errors.New(v) | ||
})(); tcErr == nil { | ||
return out, true | ||
} | ||
|
||
// case: fmt.Stringer (object with String method) | ||
if tcErr := TryCatch(func() { | ||
v := v.(fmt.Stringer) | ||
out = errors.New(v.String()) | ||
})(); tcErr == nil { | ||
return out, true | ||
} | ||
|
||
// case: []string | ||
if tcErr := TryCatch(func() { | ||
if maybeOut, okLocal := ToError(v.([]string)); okLocal { | ||
out = maybeOut | ||
} | ||
})(); tcErr == nil { | ||
return out, true | ||
} | ||
|
||
// case: []error | ||
if tcErr := TryCatch(func() { | ||
if maybeOut, okLocal := ToError(v.([]error)); okLocal { | ||
out = maybeOut | ||
} | ||
})(); tcErr == nil { | ||
return out, true | ||
} | ||
|
||
// case: []fmt.Stringer | ||
if tcErr := TryCatch(func() { | ||
if maybeOut, okLocal := ToError(v.([]fmt.Stringer)); okLocal { | ||
out = maybeOut | ||
} | ||
})(); tcErr == nil { | ||
return out, true | ||
} | ||
|
||
return fmt.Errorf("invalid type: %T", v), false | ||
} | ||
} | ||
|
||
func toErrorFromSlice(slice any) (out error, ok bool) { | ||
switch slice := slice.(type) { | ||
case []string: | ||
var errs []error | ||
for _, str := range slice { | ||
if err, okLocal := ToError(str); okLocal { | ||
errs = append(errs, err) | ||
} else { | ||
return err, false | ||
} | ||
} | ||
return CombineErrors(errs...), true | ||
case []error: | ||
return CombineErrors(slice...), true | ||
case []fmt.Stringer: | ||
var errs []error | ||
for _, stringer := range slice { | ||
if err, okLocal := ToError(stringer.String()); okLocal { | ||
errs = append(errs, err) | ||
} else { | ||
return err, false | ||
} | ||
} | ||
return CombineErrors(errs...), true | ||
} | ||
return nil, false | ||
} | ||
|
||
// Combines errors into single error. Error descriptions are ordered the same way | ||
// they're passed to the function. | ||
func CombineErrors(errs ...error) (outErr error) { | ||
for _, e := range errs { | ||
switch { | ||
case e != nil && outErr == nil: | ||
outErr = e | ||
case e != nil && outErr != nil: | ||
outErr = fmt.Errorf("%s: %s", outErr, e) | ||
} | ||
} | ||
return outErr | ||
} | ||
|
||
func CombineErrorsGeneric(errAnySlice any) (out error, ok bool) { | ||
err, ok := ToError(errAnySlice) | ||
if ok { | ||
return err, true | ||
} else { | ||
return err, false | ||
} | ||
} | ||
|
||
func CombineErrorsFromStrings(strs ...string) (err error) { | ||
var errs []error | ||
for _, s := range strs { | ||
err, _ := ToError(s) | ||
errs = append(errs, err) | ||
} | ||
return CombineErrors(errs...) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't the type switch statement catch all these cases already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's how I thought it would work at first too. The reason it skips those blocks is that
any
andinterface{}
are actual types. They don't actual mean "any type" but rather "an object of typeany
".Let's say we pass in an argument,
arg
, that fits thefmt.Stringer
interface likesdk.Coin
orsdk.AccAddress
. Whenarg
enters this function, it won't actually have the type offmt.Stringer
, so it will pass all of the case statements and end up in default.Then, when it reaches the
arg := arg.(fmt.Stringer)
block, it will enter the function withfmt.Stringer
as its value forarg.(type)
. Sincearg
actually is an instance of that fits this interface, theTryCatch
will run smoothly.However, it has to guess by manually attempting to cast as each type. Go assumes the type casting will work and forces the object to have a certain type, then it panics if something doesn't make sense, then defers, then recovers, and finally moves to the next
TryCatch
.This basically shows why Python is so slow.