1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-11 10:18:28 +09:00
anytype-heart/util/reflection/util.go

34 lines
667 B
Go

package reflection
import (
"errors"
"reflect"
"strings"
)
func GetError(obj any) (code int64, description string, err error) {
val := reflect.ValueOf(obj)
if !val.IsValid() {
return code, description, errors.New("response is absent")
}
elem := val.Elem()
for i := 0; i < elem.NumField(); i++ {
f := elem.Field(i)
if f.Kind() != reflect.Pointer {
continue
}
el := f.Elem()
if !el.IsValid() {
continue
}
if strings.Contains(el.Type().Name(), "ResponseError") {
code = el.FieldByName("Code").Int()
description = el.FieldByName("Description").String()
return
}
}
err = errors.New("can't extract the error field")
return
}