2019-06-03 20:44:43 +03:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2021-10-27 00:57:12 +03:00
|
|
|
"go.unistack.org/micro/v3/errors"
|
2019-06-03 20:44:43 +03:00
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
func microError(err error) error {
|
|
|
|
// no error
|
2020-09-18 18:27:29 +03:00
|
|
|
|
|
|
|
if err == nil {
|
2019-06-03 20:44:43 +03:00
|
|
|
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
|
2020-03-17 14:27:20 +03:00
|
|
|
s, ok := status.FromError(err)
|
|
|
|
if !ok {
|
|
|
|
return err
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
2020-03-17 14:27:20 +03:00
|
|
|
// return first error from details
|
|
|
|
if details := s.Details(); len(details) > 0 {
|
2020-07-20 12:04:51 +03:00
|
|
|
if verr, ok := details[0].(error); ok {
|
|
|
|
return microError(verr)
|
|
|
|
}
|
2020-03-17 14:27:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// try to decode micro *errors.Error
|
|
|
|
if e := errors.Parse(s.Message()); e.Code > 0 {
|
|
|
|
return e // actually a micro error
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback
|
2021-04-29 14:31:24 +03:00
|
|
|
return &errors.Error{
|
2021-10-01 01:34:16 +03:00
|
|
|
ID: "go.micro.client",
|
2021-04-29 14:31:24 +03:00
|
|
|
Code: int32(s.Code()),
|
|
|
|
Detail: s.Message(),
|
|
|
|
Status: s.Code().String(),
|
|
|
|
}
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|