Skip to content

Commit

Permalink
Forward error types as their string representation
Browse files Browse the repository at this point in the history
  • Loading branch information
sthelen-enqs committed Oct 29, 2024
1 parent 7f7579a commit 052c055
Showing 1 changed file with 20 additions and 2 deletions.
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

0 comments on commit 052c055

Please sign in to comment.