github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/remotestorage/error.go (about) 1 // Copyright 2019 Dolthub, Inc. 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 // http://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 package remotestorage 16 17 import ( 18 "encoding/json" 19 20 "google.golang.org/grpc/status" 21 ) 22 23 type RpcError struct { 24 originalErrMsg string 25 status *status.Status 26 rpc string 27 host string 28 req interface{} 29 } 30 31 func NewRpcError(err error, rpc, host string, req interface{}) *RpcError { 32 st, _ := status.FromError(err) 33 34 return &RpcError{err.Error(), st, rpc, host, req} 35 } 36 37 func (rpce *RpcError) Error() string { 38 return rpce.originalErrMsg 39 } 40 41 func (rpce *RpcError) FullDetails() string { 42 jsonStr, _ := GetJsonEncodedRequest(rpce) 43 return rpce.originalErrMsg + "\nhost:" + rpce.host + "\nrpc: " + rpce.rpc + "\nparams:" + jsonStr 44 } 45 46 func IsChunkStoreRpcErr(err error) bool { 47 _, ok := err.(*RpcError) 48 49 return ok 50 } 51 52 func GetStatus(err error) *status.Status { 53 rpce, ok := err.(*RpcError) 54 55 if !ok { 56 panic("Bug. Check IsChunkStoreRpcErr before using this") 57 } 58 59 return rpce.status 60 } 61 62 func GetRpc(err error) string { 63 rpce, ok := err.(*RpcError) 64 65 if !ok { 66 panic("Bug. Check IsChunkStoreRpcErr before using this") 67 } 68 69 return rpce.rpc 70 } 71 72 func GetHost(err error) string { 73 rpce, ok := err.(*RpcError) 74 75 if !ok { 76 panic("Bug. Check IsChunkStoreRpcErr before using this") 77 } 78 79 return rpce.host 80 } 81 82 func GetRequest(err error) interface{} { 83 rpce, ok := err.(*RpcError) 84 85 if !ok { 86 panic("Bug. Check IsChunkStoreRpcErr before using this") 87 } 88 89 return rpce.req 90 } 91 92 func GetJsonEncodedRequest(err error) (string, error) { 93 rpce, ok := err.(*RpcError) 94 95 if !ok { 96 panic("Bug. Check IsChunkStoreRpcErr before using this") 97 } 98 99 data, err := json.MarshalIndent(rpce.req, "", " ") 100 101 if err != nil { 102 return "", err 103 } 104 105 return string(data), nil 106 }