45 lines
887 B
Go
Raw Normal View History

2019-06-03 18:44:43 +01:00
package grpc
import (
pberr "github.com/unistack-org/micro-server-grpc/errors"
"github.com/unistack-org/micro/v3/errors"
2019-06-03 18:44:43 +01:00
"google.golang.org/grpc/status"
)
func microError(err error) error {
// no error
if err == nil {
2019-06-03 18:44:43 +01:00
return nil
}
if verr, ok := err.(*errors.Error); ok {
return verr
2019-06-03 18:44:43 +01:00
}
if verr, ok := err.(*pberr.Error); ok {
return &errors.Error{Id: verr.Id, Code: verr.Code, Detail: verr.Detail, Status: verr.Status}
}
2019-06-03 18:44:43 +01:00
// grpc error
s, ok := status.FromError(err)
if !ok {
return err
2019-06-03 18:44:43 +01:00
}
// return first error from details
if details := s.Details(); len(details) > 0 {
2020-07-20 10:04:51 +01:00
if verr, ok := details[0].(error); ok {
return microError(verr)
}
}
// 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 18:44:43 +01:00
}