Merge pull request #78 from unistack-org/feature2

handle more error cases
This commit is contained in:
Василий Толстов 2022-04-05 15:16:06 +03:00 committed by GitHub
commit 6d41afd5a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,32 +9,46 @@ func microError(err error) error {
// no error // no error
if err == nil { if err == nil {
// nothing to do
return nil return nil
} }
if verr, ok := err.(*errors.Error); ok { if verr, ok := err.(*errors.Error); ok {
// micro error
return verr return verr
} }
// grpc error // grpc error
s, ok := status.FromError(err) s, ok := status.FromError(err)
if !ok { if !ok {
// can't get status detals from grpc error, return base error
return err return err
} }
details := s.Details() details := s.Details()
switch len(details) { switch len(details) {
case 0: case 0:
if verr := errors.Parse(s.Message()); verr.Code > 0 {
// return micro error
return verr
}
// return base error as it not micro error
return err return err
case 1: case 1:
if verr, ok := details[0].(error); ok { if verr, ok := details[0].(*errors.Error); ok {
return microError(verr) // return nested micro error
} return verr
return err
default:
if e := errors.Parse(s.Message()); e.Code > 0 {
return e // actually a micro error
} }
// return base error as it not holds micro error
return err return err
} }
// 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
} }