github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/client/grpc/error.go (about) 1 // Copyright 2020 Asim Aslam 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Original source: github.com/micro/go-micro/v3/client/grpc/error.go 16 17 package grpc 18 19 import ( 20 "github.com/tickoalcantara12/micro/v3/service/errors" 21 "google.golang.org/grpc/status" 22 ) 23 24 func microError(err error) error { 25 // no error 26 switch err { 27 case nil: 28 return nil 29 } 30 31 if verr, ok := err.(*errors.Error); ok { 32 return verr 33 } 34 35 // grpc error 36 s, ok := status.FromError(err) 37 if !ok { 38 return err 39 } 40 41 // return first error from details 42 if details := s.Details(); len(details) > 0 { 43 if verr, ok := details[0].(error); ok { 44 return microError(verr) 45 } 46 } 47 48 // try to decode micro *errors.Error 49 if e := errors.Parse(s.Message()); e.Code > 0 { 50 return e // actually a micro error 51 } 52 53 // fallback 54 return errors.InternalServerError("go.micro.client", s.Message()) 55 }