From 2e645748bb87f61725eb18f914948c8ca978045b Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 5 Apr 2022 15:13:58 +0300 Subject: [PATCH] handle more error cases Signed-off-by: Vasiliy Tolstov --- error.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/error.go b/error.go index 57c2a5d..3b18158 100644 --- a/error.go +++ b/error.go @@ -9,32 +9,46 @@ func microError(err error) error { // no error if err == nil { + // nothing to do return nil } if verr, ok := err.(*errors.Error); ok { + // micro error return verr } // grpc error s, ok := status.FromError(err) if !ok { + // can't get status detals from grpc error, return base error return err } details := s.Details() switch len(details) { 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 case 1: - if verr, ok := details[0].(error); ok { - return microError(verr) - } - return err - default: - if e := errors.Parse(s.Message()); e.Code > 0 { - return e // actually a micro error + if verr, ok := details[0].(*errors.Error); ok { + // return nested micro error + return verr } + // return base error as it not holds micro error 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 }