code.vegaprotocol.io/vega@v0.79.0/wallet/api/node/errors.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package node 17 18 import ( 19 "fmt" 20 "strings" 21 22 typespb "code.vegaprotocol.io/vega/protos/vega" 23 24 "google.golang.org/grpc/codes" 25 "google.golang.org/grpc/status" 26 ) 27 28 type ErrorCode codes.Code 29 30 type StatusError struct { 31 Code codes.Code 32 Details []string 33 } 34 35 func (e *StatusError) Error() string { 36 if len(e.Details) == 0 { 37 return e.Code.String() 38 } 39 return fmt.Sprintf("%s: %v", e.Code.String(), strings.Join(e.Details, ", ")) 40 } 41 42 // intoStatusError extracts useful information from a gRPC status error. 43 // It returns nil if the underlying error is not a gRPC status error. 44 func intoStatusError(err error) *StatusError { 45 st, ok := status.FromError(err) 46 if !ok { 47 return nil 48 } 49 statusErr := &StatusError{ 50 Code: st.Code(), 51 Details: []string{}, 52 } 53 for _, v := range st.Details() { 54 v, ok := v.(*typespb.ErrorDetail) 55 if !ok { 56 continue 57 } 58 statusErr.Details = append(statusErr.Details, v.GetMessage()) 59 } 60 return statusErr 61 }