2019-06-03 20:44:43 +03:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2020-01-30 14:39:00 +03:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:18:33 +03:00
|
|
|
if verr, ok := err.(*errors.Error); ok {
|
|
|
|
return verr
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// grpc error
|
|
|
|
if s, ok := status.FromError(err); ok {
|
2020-02-06 13:18:33 +03:00
|
|
|
details := s.Details()
|
|
|
|
if len(details) == 0 {
|
|
|
|
if e := errors.Parse(s.Message()); e.Code > 0 {
|
|
|
|
return e // actually a micro error
|
|
|
|
}
|
|
|
|
return errors.InternalServerError("go.micro.client", s.Message())
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
2020-02-06 13:18:33 +03:00
|
|
|
// return first error from details
|
|
|
|
return details[0].(error)
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// do nothing
|
|
|
|
return err
|
|
|
|
}
|