Skip to content
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

Forward error types as their string representation #131

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions examples/remote/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@ func isExportedOrBuiltinType(t reflect.Type) bool {
return token.IsExported(t.Name()) || t.PkgPath() == ""
}

// json.Marshall won't marshall error types, marshall as string
func errorAsJson(v reflect.Value) interface{} {
if v.IsNil() {
// passthrough nil as nil, otherwise nil.(error) will panic
return nil
} else {
return v.Interface().(error).Error()
}
}

func transformReturnValues(values []reflect.Value) []interface{} {
result := make([]interface{}, len(values))

for i, e := range values {
switch e.Type() {
valueType := e.Type()

switch valueType {
case reflect.TypeFor[spineapi.DeviceRemoteInterface]():
result[i] = e.Interface().(spineapi.DeviceRemoteInterface).Address()
case reflect.TypeFor[[]spineapi.DeviceRemoteInterface]():
Expand All @@ -51,12 +63,18 @@ func transformReturnValues(values []reflect.Value) []interface{} {
}
result[i] = transformedValues
default:
result[i] = e.Interface()
switch {
case valueType.Implements(reflect.TypeFor[error]()):
result[i] = errorAsJson(e)
default:
result[i] = e.Interface()
}
}
}

return result
}

func WriteKey(cert tls.Certificate, path string) error {
file, err := os.Create(path)
if err != nil {
Expand Down
Loading