code.vegaprotocol.io/vega@v0.79.0/wallet/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 "errors" 20 "fmt" 21 "strings" 22 23 typespb "code.vegaprotocol.io/vega/protos/vega" 24 25 "google.golang.org/grpc/codes" 26 "google.golang.org/grpc/status" 27 ) 28 29 var ErrNoHostSpecified = errors.New("no host specified") 30 31 type ErrorCode codes.Code 32 33 type StatusError struct { 34 Code codes.Code 35 Details []string 36 } 37 38 func (e *StatusError) Error() string { 39 return fmt.Sprintf("%s - %v", e.Code.String(), strings.Join(e.Details, ", ")) 40 } 41 42 // intoStatusError extract useful information from a gRPC status error. 43 // 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 }