github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/util/errorutil/unimplemented/unimplemented.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 unimplemented
    12  
    13  import (
    14  	"fmt"
    15  
    16  	"github.com/cockroachdb/cockroachdb-parser/pkg/build"
    17  	"github.com/cockroachdb/cockroachdb-parser/pkg/sql/pgwire/pgcode"
    18  	"github.com/cockroachdb/cockroachdb-parser/pkg/sql/pgwire/pgerror"
    19  	"github.com/cockroachdb/errors"
    20  )
    21  
    22  // This file re-implements the unimplemented primitives from the
    23  // original pgerror package, using the primitives from the errors
    24  // library instead.
    25  
    26  // New constructs an unimplemented feature error.
    27  func New(feature, msg string) error {
    28  	return unimplementedInternal(1 /*depth*/, 0 /*issue*/, feature /*detail*/, false /*format*/, msg)
    29  }
    30  
    31  // Newf constructs an unimplemented feature error.
    32  // The message is formatted.
    33  func Newf(feature, format string, args ...interface{}) error {
    34  	return NewWithDepthf(1, feature, format, args...)
    35  }
    36  
    37  // NewWithDepthf constructs an implemented feature error,
    38  // tracking the context at the specified depth.
    39  func NewWithDepthf(depth int, feature, format string, args ...interface{}) error {
    40  	return unimplementedInternal(depth+1 /*depth*/, 0 /*issue*/, feature /*detail*/, true /*format*/, format, args...)
    41  }
    42  
    43  // NewWithIssue constructs an error with the given message
    44  // and a link to the passed issue. Recorded as "#<issue>" in tracking.
    45  func NewWithIssue(issue int, msg string) error {
    46  	return unimplementedInternal(1 /*depth*/, issue, "" /*detail*/, false /*format*/, msg)
    47  }
    48  
    49  // NewWithIssuef constructs an error with the formatted message
    50  // and a link to the passed issue. Recorded as "#<issue>" in tracking.
    51  func NewWithIssuef(issue int, format string, args ...interface{}) error {
    52  	return unimplementedInternal(1 /*depth*/, issue, "" /*detail*/, true /*format*/, format, args...)
    53  }
    54  
    55  // NewWithIssueDetail constructs an error with the given message
    56  // and a link to the passed issue. Recorded as "#<issue>.detail" in tracking.
    57  // This is useful when we need an extra axis of information to drill down into.
    58  func NewWithIssueDetail(issue int, detail, msg string) error {
    59  	return unimplementedInternal(1 /*depth*/, issue, detail, false /*format*/, msg)
    60  }
    61  
    62  // NewWithIssueDetailf is like NewWithIssueDetail but the message is formatted.
    63  func NewWithIssueDetailf(issue int, detail, format string, args ...interface{}) error {
    64  	return unimplementedInternal(1 /*depth*/, issue, detail, true /*format*/, format, args...)
    65  }
    66  
    67  func unimplementedInternal(
    68  	depth, issue int, detail string, format bool, msg string, args ...interface{},
    69  ) error {
    70  	// Create the issue link.
    71  	link := errors.IssueLink{Detail: detail}
    72  	if issue > 0 {
    73  		link.IssueURL = build.MakeIssueURL(issue)
    74  	}
    75  
    76  	// Instantiate the base error.
    77  	var err error
    78  	if format {
    79  		err = errors.UnimplementedErrorf(link, "unimplemented: "+msg, args...)
    80  		err = errors.WithSafeDetails(err, msg, args...)
    81  	} else {
    82  		err = errors.UnimplementedError(link, "unimplemented: "+msg)
    83  	}
    84  	// Decorate with a stack trace.
    85  	err = errors.WithStackDepth(err, 1+depth)
    86  
    87  	if issue > 0 {
    88  		// There is an issue number. Decorate with a telemetry annotation.
    89  		var key string
    90  		if detail == "" {
    91  			key = fmt.Sprintf("#%d", issue)
    92  		} else {
    93  			key = fmt.Sprintf("#%d.%s", issue, detail)
    94  		}
    95  		err = errors.WithTelemetry(err, key)
    96  	} else if detail != "" {
    97  		// No issue but a detail string. It's an indication to also
    98  		// perform telemetry.
    99  		err = errors.WithTelemetry(err, detail)
   100  	}
   101  
   102  	// Wrap with the corresponding PG error for unimplemented.
   103  	err = pgerror.WithCandidateCode(err, pgcode.FeatureNotSupported)
   104  	return err
   105  }