github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/scrub/errors.go (about)

     1  // Copyright 2017 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 scrub
    12  
    13  import (
    14  	"fmt"
    15  
    16  	"github.com/cockroachdb/errors"
    17  )
    18  
    19  const (
    20  	// MissingIndexEntryError occurs when a primary k/v is missing a
    21  	// corresponding secondary index k/v.
    22  	MissingIndexEntryError = "missing_index_entry"
    23  	// DanglingIndexReferenceError occurs when a secondary index k/v
    24  	// points to a non-existing primary k/v.
    25  	DanglingIndexReferenceError = "dangling_index_reference"
    26  	// PhysicalError is a generic error when there is an error in the
    27  	// SQL physical data.
    28  	PhysicalError = "physical_error"
    29  	// IndexKeyDecodingError occurs while decoding the an index key.
    30  	IndexKeyDecodingError = "index_key_decoding_error"
    31  	// IndexValueDecodingError occurs while decoding the an index value.
    32  	IndexValueDecodingError = "index_value_decoding_error"
    33  	// SecondaryIndexKeyExtraValueDecodingError occurs when the extra
    34  	// columns stored in a key fail to decode.
    35  	SecondaryIndexKeyExtraValueDecodingError = "secondary_index_key_extra_value_decoding_error"
    36  	// UnexpectedNullValueError occurs when a null value is encountered where the
    37  	// value is expected to be non-nullable.
    38  	UnexpectedNullValueError = "null_value_error"
    39  	// CheckConstraintViolation occurs when a row in a table is
    40  	// violating a check constraint.
    41  	CheckConstraintViolation = "check_constraint_violation"
    42  	// ForeignKeyConstraintViolation occurs when a row in a
    43  	// table is violating a foreign key constraint.
    44  	ForeignKeyConstraintViolation = "foreign_key_violation"
    45  )
    46  
    47  // Error contains the details on the scrub error that was caught.
    48  type Error struct {
    49  	Code       string
    50  	underlying error
    51  }
    52  
    53  func (s *Error) Error() string {
    54  	return fmt.Sprintf("%s: %+v", s.Code, s.underlying)
    55  }
    56  
    57  // Cause unwraps the error.
    58  func (s *Error) Cause() error {
    59  	return s.underlying
    60  }
    61  
    62  // Format implements fmt.Formatter.
    63  func (s *Error) Format(st fmt.State, verb rune) { errors.FormatError(s, st, verb) }
    64  
    65  // WrapError wraps an error with a Error.
    66  func WrapError(code string, err error) *Error {
    67  	return &Error{
    68  		Code:       code,
    69  		underlying: err,
    70  	}
    71  }
    72  
    73  // IsScrubError checks if an error is a Error.
    74  func IsScrubError(err error) bool {
    75  	return errors.HasType(err, (*Error)(nil))
    76  }
    77  
    78  // UnwrapScrubError gets the underlying error if err is a scrub.Error.
    79  // If err is not a scrub.Error nil is returned.
    80  func UnwrapScrubError(err error) error {
    81  	var e *Error
    82  	if errors.As(err, &e) {
    83  		return e.underlying
    84  	}
    85  	return err
    86  }