github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/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) IsPermanent() bool { 42 return statusCodeIsPermanentError(rpce.status) 43 } 44 45 func (rpce *RpcError) FullDetails() string { 46 jsonStr, _ := GetJsonEncodedRequest(rpce) 47 return rpce.originalErrMsg + "\nhost:" + rpce.host + "\nrpc: " + rpce.rpc + "\nparams:" + jsonStr 48 } 49 50 func IsChunkStoreRpcErr(err error) bool { 51 _, ok := err.(*RpcError) 52 53 return ok 54 } 55 56 func GetStatus(err error) *status.Status { 57 rpce, ok := err.(*RpcError) 58 59 if !ok { 60 panic("Bug. Check IsChunkStoreRpcErr before using this") 61 } 62 63 return rpce.status 64 } 65 66 func GetRpc(err error) string { 67 rpce, ok := err.(*RpcError) 68 69 if !ok { 70 panic("Bug. Check IsChunkStoreRpcErr before using this") 71 } 72 73 return rpce.rpc 74 } 75 76 func GetHost(err error) string { 77 rpce, ok := err.(*RpcError) 78 79 if !ok { 80 panic("Bug. Check IsChunkStoreRpcErr before using this") 81 } 82 83 return rpce.host 84 } 85 86 func GetRequest(err error) interface{} { 87 rpce, ok := err.(*RpcError) 88 89 if !ok { 90 panic("Bug. Check IsChunkStoreRpcErr before using this") 91 } 92 93 return rpce.req 94 } 95 96 func GetJsonEncodedRequest(err error) (string, error) { 97 rpce, ok := err.(*RpcError) 98 99 if !ok { 100 panic("Bug. Check IsChunkStoreRpcErr before using this") 101 } 102 103 data, err := json.MarshalIndent(rpce.req, "", " ") 104 105 if err != nil { 106 return "", err 107 } 108 109 return string(data), nil 110 }