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 15package internal 16 17import ( 18 "errors" 19 "testing" 20 21 "google.golang.org/api/googleapi" 22 "google.golang.org/grpc/codes" 23 "google.golang.org/grpc/status" 24) 25 26const wantMessage = "prefix: msg" 27 28func TestAnnotateGRPC(t *testing.T) { 29 // grpc Status error 30 err := status.Error(codes.NotFound, "msg") 31 err = Annotate(err, "prefix") 32 got, ok := status.FromError(err) 33 if !ok { 34 t.Fatalf("got %T, wanted a status", got) 35 } 36 if g, w := got.Code(), codes.NotFound; g != w { 37 t.Errorf("got code %v, want %v", g, w) 38 } 39 if g, w := got.Message(), wantMessage; g != w { 40 t.Errorf("got message %q, want %q", g, w) 41 } 42} 43 44func TestAnnotateGoogleapi(t *testing.T) { 45 // googleapi error 46 var err error = &googleapi.Error{Code: 403, Message: "msg"} 47 err = Annotate(err, "prefix") 48 got2, ok := err.(*googleapi.Error) 49 if !ok { 50 t.Fatalf("got %T, wanted a googleapi.Error", got2) 51 } 52 if g, w := got2.Code, 403; g != w { 53 t.Errorf("got code %d, want %d", g, w) 54 } 55 if g, w := got2.Message, wantMessage; g != w { 56 t.Errorf("got message %q, want %q", g, w) 57 } 58} 59 60func TestAnnotateUnknownError(t *testing.T) { 61 err := Annotate(errors.New("msg"), "prefix") 62 if g, w := err.Error(), wantMessage; g != w { 63 t.Errorf("got message %q, want %q", g, w) 64 } 65} 66