2019-06-03 19:44:43 +02:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2021-01-20 23:01:10 +02:00
|
|
|
"github.com/asim/go-micro/v3/errors"
|
2019-06-03 19:44:43 +02:00
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
func microError(err error) error {
|
|
|
|
// no error
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-06 12:18:33 +02:00
|
|
|
if verr, ok := err.(*errors.Error); ok {
|
|
|
|
return verr
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// grpc error
|
2020-03-17 13:27:20 +02:00
|
|
|
s, ok := status.FromError(err)
|
|
|
|
if !ok {
|
|
|
|
return err
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
2020-03-17 13:27:20 +02: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
|
2021-04-29 14:03:40 +02:00
|
|
|
return &errors.Error{
|
|
|
|
Id: "go.micro.client",
|
|
|
|
Code: int32(s.Code()),
|
|
|
|
Detail: s.Message(),
|
|
|
|
Status: s.Code().String(),
|
|
|
|
}
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|