1/* 2Copyright 2017 Google LLC 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package spanner 18 19import ( 20 "context" 21 "errors" 22 "strings" 23 "testing" 24 25 "google.golang.org/grpc/codes" 26 "google.golang.org/grpc/status" 27) 28 29type wrappedTestError struct { 30 wrapped error 31 msg string 32} 33 34func (w *wrappedTestError) Error() string { 35 return w.msg 36} 37 38func (w *wrappedTestError) Unwrap() error { 39 return w.wrapped 40} 41 42func TestToSpannerError(t *testing.T) { 43 for _, test := range []struct { 44 err error 45 wantCode codes.Code 46 wantMsg string 47 wantWrappedError error 48 }{ 49 {errors.New("wha?"), codes.Unknown, `spanner: code = "Unknown", desc = "wha?"`, errors.New("wha?")}, 50 {context.Canceled, codes.Canceled, `spanner: code = "Canceled", desc = "context canceled"`, status.Errorf(codes.Canceled, "context canceled")}, 51 {context.DeadlineExceeded, codes.DeadlineExceeded, `spanner: code = "DeadlineExceeded", desc = "context deadline exceeded"`, status.Errorf(codes.DeadlineExceeded, "context deadline exceeded")}, 52 {status.Errorf(codes.ResourceExhausted, "so tired"), codes.ResourceExhausted, `spanner: code = "ResourceExhausted", desc = "so tired"`, status.Errorf(codes.ResourceExhausted, "so tired")}, 53 {spannerErrorf(codes.InvalidArgument, "bad"), codes.InvalidArgument, `spanner: code = "InvalidArgument", desc = "bad"`, status.Errorf(codes.InvalidArgument, "bad")}, 54 {&wrappedTestError{ 55 wrapped: spannerErrorf(codes.Aborted, "Transaction aborted"), 56 msg: "error with wrapped Spanner error", 57 }, codes.Aborted, `spanner: code = "Aborted", desc = "Transaction aborted"`, status.Errorf(codes.Aborted, "Transaction aborted")}, 58 {&wrappedTestError{ 59 wrapped: errors.New("wha?"), 60 msg: "error with wrapped non-gRPC and non-Spanner error", 61 }, codes.Unknown, `spanner: code = "Unknown", desc = "error with wrapped non-gRPC and non-Spanner error"`, 62 &wrappedTestError{ 63 wrapped: errors.New("wha?"), 64 msg: "error with wrapped non-gRPC and non-Spanner error"}}, 65 } { 66 err := toSpannerError(test.err) 67 errDuringCommit := toSpannerErrorWithCommitInfo(test.err, true) 68 if got, want := ErrCode(err), test.wantCode; got != want { 69 t.Errorf("%v: got %s, want %s", test.err, got, want) 70 } 71 converted := status.Convert(err) 72 if converted.Code() != test.wantCode { 73 t.Errorf("%v: got status %v, want status %v", test.err, converted.Code(), test.wantCode) 74 } 75 if got, want := err.Error(), test.wantMsg; got != want { 76 t.Errorf("%v: got msg %s, want mgs %s", test.err, got, want) 77 } 78 if got, want := err.(*Error).err, test.wantWrappedError; got.Error() != want.Error() { 79 t.Errorf("%v: Wrapped mismatch\nGot: %v\nWant: %v", test.err, got, want) 80 } 81 code := status.Code(errDuringCommit) 82 gotWrappedDuringCommit := errDuringCommit.(*Error).err 83 // Only DEADLINE_EXCEEDED and CANCELED should indicate that the 84 // transaction outcome is unknown. 85 if code == codes.DeadlineExceeded || code == codes.Canceled { 86 if !strings.Contains(errDuringCommit.Error(), transactionOutcomeUnknownMsg) { 87 t.Errorf(`%v: Missing %q from error during commit.\nGot: %v`, test.err, transactionOutcomeUnknownMsg, errDuringCommit) 88 } 89 wantWrappedDuringCommit := &TransactionOutcomeUnknownError{} 90 if gotWrappedDuringCommit.Error() != wantWrappedDuringCommit.Error() { 91 t.Errorf("%v: Wrapped commit error mismatch\nGot: %v\nWant: %v", test.err, gotWrappedDuringCommit, wantWrappedDuringCommit) 92 } 93 } else { 94 if strings.Contains(errDuringCommit.Error(), transactionOutcomeUnknownMsg) { 95 t.Errorf(`%v: Got unexpected %q in error during commit.\nGot: %v`, test.err, transactionOutcomeUnknownMsg, errDuringCommit) 96 } 97 wantWrappedDuringCommit := test.wantWrappedError 98 if gotWrappedDuringCommit.Error() != wantWrappedDuringCommit.Error() { 99 t.Errorf("%v: Wrapped commit error mismatch\nGot: %v\nWant: %v", test.err, gotWrappedDuringCommit, wantWrappedDuringCommit) 100 } 101 } 102 } 103} 104