github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/pgwire/pgerror/with_candidate_code.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 12 13 import ( 14 "context" 15 "fmt" 16 17 "github.com/cockroachdb/cockroachdb-parser/pkg/sql/pgwire/pgcode" 18 "github.com/cockroachdb/errors" 19 "github.com/gogo/protobuf/proto" 20 ) 21 22 type withCandidateCode struct { 23 cause error 24 code string 25 } 26 27 var _ error = (*withCandidateCode)(nil) 28 var _ errors.SafeDetailer = (*withCandidateCode)(nil) 29 var _ fmt.Formatter = (*withCandidateCode)(nil) 30 var _ errors.SafeFormatter = (*withCandidateCode)(nil) 31 32 func (w *withCandidateCode) Error() string { return w.cause.Error() } 33 func (w *withCandidateCode) Cause() error { return w.cause } 34 func (w *withCandidateCode) Unwrap() error { return w.cause } 35 func (w *withCandidateCode) SafeDetails() []string { return []string{w.code} } 36 37 func (w *withCandidateCode) Format(s fmt.State, verb rune) { errors.FormatError(w, s, verb) } 38 39 func (w *withCandidateCode) SafeFormatError(p errors.Printer) (next error) { 40 if p.Detail() { 41 p.Printf("candidate pg code: %s", errors.Safe(w.code)) 42 } 43 return w.cause 44 } 45 46 // decodeWithCandidateCode is a custom decoder that will be used when decoding 47 // withCandidateCode error objects. 48 // Note that as the last argument it takes proto.Message (and not 49 // protoutil.Message which is required by linter) because the latter brings in 50 // additional dependencies into this package and the former is sufficient here. 51 func decodeWithCandidateCode( 52 _ context.Context, cause error, _ string, details []string, _ proto.Message, 53 ) error { 54 code := pgcode.Uncategorized.String() 55 if len(details) > 0 { 56 code = details[0] 57 } 58 return &withCandidateCode{cause: cause, code: code} 59 } 60 61 func init() { 62 errors.RegisterWrapperDecoder(errors.GetTypeKey((*withCandidateCode)(nil)), decodeWithCandidateCode) 63 }