vitess.io/vitess@v0.16.2/go/vt/vterrors/grpc.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package vterrors 18 19 import ( 20 "fmt" 21 "io" 22 23 "google.golang.org/grpc/codes" 24 "google.golang.org/grpc/status" 25 26 vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" 27 ) 28 29 // This file contains functions to convert errors to and from gRPC codes. 30 // Use these methods to return an error through gRPC and still 31 // retain its code. 32 33 // truncateError shortens errors because gRPC has a size restriction on them. 34 func truncateError(err error) string { 35 // For more details see: https://github.com/grpc/grpc-go/issues/443 36 // The gRPC spec says "Clients may limit the size of Response-Headers, 37 // Trailers, and Trailers-Only, with a default of 8 KiB each suggested." 38 // Therefore, we assume 8 KiB minus some headroom. 39 GRPCErrorLimit := 8*1024 - 512 40 if len(err.Error()) <= GRPCErrorLimit { 41 return err.Error() 42 } 43 truncateInfo := "[...] [remainder of the error is truncated because gRPC has a size limit on errors.]" 44 truncatedErr := err.Error()[:GRPCErrorLimit] 45 return fmt.Sprintf("%v %v", truncatedErr, truncateInfo) 46 } 47 48 // ToGRPC returns an error as a gRPC error, with the appropriate error code. 49 func ToGRPC(err error) error { 50 if err == nil { 51 return nil 52 } 53 return status.Errorf(codes.Code(Code(err)), "%v", truncateError(err)) 54 } 55 56 // FromGRPC returns a gRPC error as a vtError, translating between error codes. 57 // However, there are a few errors which are not translated and passed as they 58 // are. For example, io.EOF since our code base checks for this error to find 59 // out that a stream has finished. 60 func FromGRPC(err error) error { 61 if err == nil { 62 return nil 63 } 64 if err == io.EOF { 65 // Do not wrap io.EOF because we compare against it for finished streams. 66 return err 67 } 68 code := codes.Unknown 69 if s, ok := status.FromError(err); ok { 70 code = s.Code() 71 } 72 return New(vtrpcpb.Code(code), err.Error()) 73 }