github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/geo/errors.go (about)

     1  // Copyright 2020 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 geo
    12  
    13  import (
    14  	"fmt"
    15  
    16  	"github.com/cockroachdb/cockroachdb-parser/pkg/geo/geopb"
    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  // NewMismatchingSRIDsError returns the error message for SRIDs of GeospatialTypes
    23  // a and b being a mismatch.
    24  func NewMismatchingSRIDsError(a geopb.SpatialObject, b geopb.SpatialObject) error {
    25  	return pgerror.Newf(
    26  		pgcode.InvalidParameterValue,
    27  		"operation on mixed SRIDs forbidden: (%s, %d) != (%s, %d)",
    28  		a.ShapeType,
    29  		a.SRID,
    30  		b.ShapeType,
    31  		b.SRID,
    32  	)
    33  }
    34  
    35  // OutOfRangeError returns the error message to use if a spatial object contains
    36  // an NaN coordinate. This is the same message that Postgres uses.
    37  func OutOfRangeError() error {
    38  	return pgerror.Newf(
    39  		pgcode.InvalidParameterValue, "input is out of range",
    40  	)
    41  }
    42  
    43  // EmptyGeometryError is an error that is returned when the Geometry or any
    44  // parts of its subgeometries are empty.
    45  type EmptyGeometryError struct {
    46  	cause error
    47  }
    48  
    49  var _ error = (*EmptyGeometryError)(nil)
    50  var _ errors.SafeDetailer = (*EmptyGeometryError)(nil)
    51  var _ fmt.Formatter = (*EmptyGeometryError)(nil)
    52  var _ errors.Formatter = (*EmptyGeometryError)(nil)
    53  
    54  // Error implements the error interface.
    55  func (w *EmptyGeometryError) Error() string { return w.cause.Error() }
    56  
    57  // Cause implements the errors.SafeDetailer interface.
    58  func (w *EmptyGeometryError) Cause() error { return w.cause }
    59  
    60  // Unwrap implements the SafeDetailer interface.
    61  func (w *EmptyGeometryError) Unwrap() error { return w.cause }
    62  
    63  // SafeDetails implements the SafeDetailer interface.
    64  func (w *EmptyGeometryError) SafeDetails() []string { return []string{w.cause.Error()} }
    65  
    66  // Format implements the errors.Formatter interface.
    67  func (w *EmptyGeometryError) Format(s fmt.State, verb rune) { errors.FormatError(w, s, verb) }
    68  
    69  // FormatError implements the errors.Formatter interface.
    70  func (w *EmptyGeometryError) FormatError(p errors.Printer) (next error) { return w.cause }
    71  
    72  // IsEmptyGeometryError returns true if the error is of type EmptyGeometryError.
    73  func IsEmptyGeometryError(err error) bool {
    74  	return errors.HasType(err, &EmptyGeometryError{})
    75  }
    76  
    77  // NewEmptyGeometryError returns an error indicating an empty geometry has been found.
    78  func NewEmptyGeometryError() *EmptyGeometryError {
    79  	return &EmptyGeometryError{cause: pgerror.Newf(pgcode.InvalidParameterValue, "empty shape found")}
    80  }