micro/client/grpc/error.go

38 lines
659 B
Go
Raw Normal View History

2019-06-03 20:44:43 +03:00
package grpc
import (
"github.com/micro/go-micro/v2/errors"
2019-06-03 20:44:43 +03:00
"google.golang.org/grpc/status"
)
func microError(err error) error {
// no error
switch err {
case nil:
return nil
}
if verr, ok := err.(*errors.Error); ok {
return verr
2019-06-03 20:44:43 +03:00
}
// grpc error
s, ok := status.FromError(err)
if !ok {
return err
2019-06-03 20:44:43 +03:00
}
// return first error from details
if details := s.Details(); len(details) > 0 {
return microError(details[0].(error))
}
// try to decode micro *errors.Error
if e := errors.Parse(s.Message()); e.Code > 0 {
return e // actually a micro error
}
// fallback
return errors.InternalServerError("go.micro.client", s.Message())
2019-06-03 20:44:43 +03:00
}