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 {
|
2022-04-05 15:13:58 +03:00
|
|
|
// nothing to do
|
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 {
|
2022-04-05 15:13:58 +03:00
|
|
|
// micro error
|
2020-02-06 13:18:33 +03:00
|
|
|
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 {
|
2022-04-05 15:13:58 +03:00
|
|
|
// can't get status detals from grpc error, return base error
|
2020-03-17 14:27:20 +03:00
|
|
|
return err
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
2022-04-05 12:36:14 +03:00
|
|
|
details := s.Details()
|
|
|
|
switch len(details) {
|
|
|
|
case 0:
|
2022-04-05 15:13:58 +03:00
|
|
|
if verr := errors.Parse(s.Message()); verr.Code > 0 {
|
|
|
|
// return micro error
|
|
|
|
return verr
|
2020-07-20 12:04:51 +03:00
|
|
|
}
|
2022-04-05 15:13:58 +03:00
|
|
|
// return base error as it not micro error
|
2022-04-05 12:36:14 +03:00
|
|
|
return err
|
2022-04-05 15:13:58 +03:00
|
|
|
case 1:
|
|
|
|
if verr, ok := details[0].(*errors.Error); ok {
|
|
|
|
// return nested micro error
|
|
|
|
return verr
|
2022-04-05 12:36:14 +03:00
|
|
|
}
|
2022-04-05 15:13:58 +03:00
|
|
|
// return base error as it not holds micro error
|
2022-04-05 12:36:14 +03:00
|
|
|
return err
|
2021-04-29 14:31:24 +03:00
|
|
|
}
|
2022-04-05 15:13:58 +03:00
|
|
|
|
|
|
|
// attached messages in details more then 1, try to fallback to micro error
|
|
|
|
if verr := errors.Parse(s.Message()); verr.Code > 0 {
|
|
|
|
// return micro error
|
|
|
|
return verr
|
|
|
|
}
|
|
|
|
|
|
|
|
// not micro error return base error
|
|
|
|
return err
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|