github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/pgwire/pgerror/flatten_test.go (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package pgerror_test 12 13 import ( 14 "testing" 15 16 "github.com/cockroachdb/cockroach/pkg/roachpb" 17 "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" 18 "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" 19 "github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented" 20 "github.com/cockroachdb/errors" 21 "github.com/cockroachdb/errors/testutils" 22 ) 23 24 func TestFlatten(t *testing.T) { 25 baseErr := errors.New("woo") 26 27 testData := []struct { 28 err error 29 checker func(t testutils.T, pgErr *pgerror.Error) 30 }{ 31 { 32 baseErr, 33 func(t testutils.T, e *pgerror.Error) { 34 t.CheckEqual(e.Message, "woo") 35 // Errors without code flatten to Uncategorized. 36 t.CheckEqual(e.Code, pgcode.Uncategorized) 37 t.CheckEqual(e.Severity, "ERROR") 38 }, 39 }, 40 { 41 pgerror.WithCandidateCode(baseErr, pgcode.Syntax), 42 func(t testutils.T, e *pgerror.Error) { 43 t.CheckEqual(e.Message, "woo") 44 t.CheckEqual(e.Code, pgcode.Syntax) 45 }, 46 }, 47 { 48 pgerror.WithSeverity(baseErr, "DEBUG"), 49 func(t testutils.T, e *pgerror.Error) { 50 t.CheckEqual(e.Message, "woo") 51 t.CheckEqual(e.Severity, "DEBUG") 52 }, 53 }, 54 { 55 errors.WithHint(baseErr, "My Hint"), 56 func(t testutils.T, e *pgerror.Error) { 57 t.CheckEqual(e.Message, "woo") 58 t.CheckEqual(e.Hint, "My Hint") 59 }, 60 }, 61 { 62 errors.WithDetail(baseErr, "My Detail"), 63 func(t testutils.T, e *pgerror.Error) { 64 t.CheckEqual(e.Message, "woo") 65 t.CheckEqual(e.Detail, "My Detail") 66 }, 67 }, 68 { 69 unimplemented.New("woo", "woo"), 70 func(t testutils.T, e *pgerror.Error) { 71 t.CheckEqual(e.Code, pgcode.FeatureNotSupported) 72 t.CheckRegexpEqual(e.Hint, "You have attempted to use a feature that is not yet implemented") 73 t.CheckRegexpEqual(e.Hint, "support form") 74 }, 75 }, 76 { 77 errors.AssertionFailedf("woo"), 78 func(t testutils.T, e *pgerror.Error) { 79 t.CheckEqual(e.Code, pgcode.Internal) 80 t.CheckRegexpEqual(e.Hint, "You have encountered an unexpected error") 81 t.CheckRegexpEqual(e.Hint, "support form") 82 }, 83 }, 84 { 85 errors.Wrap(&roachpb.TransactionRetryWithProtoRefreshError{Msg: "woo"}, ""), 86 func(t testutils.T, e *pgerror.Error) { 87 t.CheckRegexpEqual(e.Message, "restart transaction: .* woo") 88 t.CheckEqual(e.Code, pgcode.SerializationFailure) 89 }, 90 }, 91 { 92 errors.Wrap(&roachpb.AmbiguousResultError{Message: "woo"}, ""), 93 func(t testutils.T, e *pgerror.Error) { 94 t.CheckRegexpEqual(e.Message, "result is ambiguous.*woo") 95 t.CheckEqual(e.Code, pgcode.StatementCompletionUnknown) 96 }, 97 }, 98 } 99 tt := testutils.T{T: t} 100 101 for _, test := range testData { 102 // tt.Logf("input error: %# v", pretty.Formatter(test.err)) 103 pgErr := pgerror.Flatten(test.err) 104 // tt.Logf("pg error: %# v", pretty.Formatter(pgErr)) 105 106 // Common checks for all errors. 107 tt.CheckEqual(pgErr.Source.File, "flatten_test.go") 108 tt.CheckEqual(pgErr.Source.Function, "TestFlatten") 109 110 // Per-test specific checks. 111 test.checker(tt, pgErr) 112 } 113 }