github.com/cockroachdb/errors@v1.11.1/errbase/opaque_test.go (about) 1 // Copyright 2023 The Cockroach Authors. 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 12 // implied. See the License for the specific language governing 13 // permissions and limitations under the License. 14 15 package errbase 16 17 import ( 18 "context" 19 "errors" 20 "fmt" 21 "testing" 22 23 "github.com/cockroachdb/errors/testutils" 24 "github.com/kr/pretty" 25 ) 26 27 func TestUnknownWrapperTraversalWithMessageOverride(t *testing.T) { 28 // Simulating scenario where the new field on the opaque wrapper is dropped 29 // in the middle of the chain by a node running an older version. 30 31 origErr := fmt.Errorf("this is a wrapped err %w with a non-prefix wrap msg", errors.New("hello")) 32 t.Logf("start err: %# v", pretty.Formatter(origErr)) 33 34 // Encode the error, this will use the encoder. 35 enc := EncodeError(context.Background(), origErr) 36 t.Logf("encoded: %# v", pretty.Formatter(enc)) 37 38 newErr := DecodeError(context.Background(), enc) 39 t.Logf("decoded: %# v", pretty.Formatter(newErr)) 40 41 // simulate node not knowing about `messageType` field 42 newErr.(*opaqueWrapper).messageType = Prefix 43 44 // Encode it again, to simulate the error passed on to another system. 45 enc2 := EncodeError(context.Background(), newErr) 46 t.Logf("encoded2: %# v", pretty.Formatter(enc)) 47 48 // Then decode again. 49 newErr2 := DecodeError(context.Background(), enc2) 50 t.Logf("decoded: %# v", pretty.Formatter(newErr2)) 51 52 tt := testutils.T{T: t} 53 54 // We expect to see an erroneous `: hello` because our 55 // error passes through a node which drops the new protobuf 56 // field. 57 tt.CheckEqual(newErr2.Error(), origErr.Error()+": hello") 58 }