1 // Copyright 2017 Google LLC 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 internal 16 17 import ( 18 "fmt" 19 20 "google.golang.org/api/googleapi" 21 "google.golang.org/grpc/status" 22 ) 23 24 // Annotate prepends msg to the error message in err, attempting 25 // to preserve other information in err, like an error code. 26 // 27 // Annotate panics if err is nil. 28 // 29 // Annotate knows about these error types: 30 // - "google.golang.org/grpc/status".Status 31 // - "google.golang.org/api/googleapi".Error 32 // If the error is not one of these types, Annotate behaves 33 // like 34 // 35 // fmt.Errorf("%s: %v", msg, err) 36 func Annotate(err error, msg string) error { 37 if err == nil { 38 panic("Annotate called with nil") 39 } 40 if s, ok := status.FromError(err); ok { 41 p := s.Proto() 42 p.Message = msg + ": " + p.Message 43 return status.ErrorProto(p) 44 } 45 if g, ok := err.(*googleapi.Error); ok { 46 g.Message = msg + ": " + g.Message 47 return g 48 } 49 return fmt.Errorf("%s: %v", msg, err) 50 } 51 52 // Annotatef uses format and args to format a string, then calls Annotate. 53 func Annotatef(err error, format string, args ...interface{}) error { 54 return Annotate(err, fmt.Sprintf(format, args...)) 55 } 56