gofail icon indicating copy to clipboard operation
gofail copied to clipboard

How to use failpoint to return two values?

Open atmzhou opened this issue 8 years ago • 3 comments

A go function may return multiple values, such as func a() (string, int). In this case, how should I define failpoint variables?

Thank you!

atmzhou avatar Nov 15 '17 08:11 atmzhou

Failpoints by convention only set a single value-- they're not functions. What's your use case?

heyitsanthony avatar Nov 15 '17 09:11 heyitsanthony

A go function may return some error code and I want to use fail point to simulate that the function returns an error. But the error code may be returned with other variables together, as a go function allows to return multiple variables. Thanks!

atmzhou avatar Nov 15 '17 14:11 atmzhou

Marshall it into a string:

func whatever() (*someStruct, error) {
...
// gofail: var someData string
// return unmarshalMyData(someData)
...
}

func unmarshalMyData(v string) (*someStruct, error) { ... }

In general there needs to be some custom unmarshalling to return user-provided errors since many packages use singleton error values (e.g., io.EOF) for pointer comparisons. There's no way to get that through reflection as far as I know.

I wouldn't advise extending gofail to support multi-value failpoints directly since it would introduce a lot of complexity. Maybe supporting %q-formatted gob decoding for arbitrary structs would be OK as part of the rewrite rules.

heyitsanthony avatar Nov 15 '17 15:11 heyitsanthony

No plan to support this.

ahrtr avatar Dec 02 '22 20:12 ahrtr