You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

66 lines
1.8 KiB

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