github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/datacodec/errors.go (about)

     1  // Copyright 2021 DataStax
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package datacodec
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"reflect"
    21  
    22  	"github.com/datastax/go-cassandra-native-protocol/datatype"
    23  	"github.com/datastax/go-cassandra-native-protocol/primitive"
    24  )
    25  
    26  var ErrNilDestination = errors.New("destination is nil")
    27  var ErrNilDataType = errors.New("data type is nil")
    28  
    29  var ErrConversionNotSupported = errors.New("conversion not supported")
    30  var ErrSourceTypeNotSupported = errors.New("source type not supported")
    31  var ErrDestinationTypeNotSupported = errors.New("destination type not supported")
    32  
    33  var ErrPointerTypeExpected = errors.New("destination is not pointer")
    34  
    35  func errCannotEncode(source interface{}, dataType datatype.DataType, version primitive.ProtocolVersion, err error) error {
    36  	return fmt.Errorf("cannot encode %T as CQL %s with %v: %w", source, dataType, version, err)
    37  }
    38  
    39  func errCannotDecode(dest interface{}, dataType datatype.DataType, version primitive.ProtocolVersion, err error) error {
    40  	return fmt.Errorf("cannot decode CQL %s as %T with %v: %w", dataType, dest, version, err)
    41  }
    42  
    43  func errDataTypeNotSupported(dataType datatype.DataType, version primitive.ProtocolVersion) error {
    44  	return fmt.Errorf("data type %s not supported in %v", dataType, version)
    45  }
    46  
    47  func errSourceConversionFailed(from interface{}, to interface{}, err error) error {
    48  	return fmt.Errorf("cannot convert from %T to %T: %w", from, to, err)
    49  }
    50  
    51  func errDestinationConversionFailed(from interface{}, to interface{}, err error) error {
    52  	return fmt.Errorf("cannot convert from %T to %T: %w", from, to, err)
    53  }
    54  
    55  func errCannotRead(val interface{}, err error) error {
    56  	return fmt.Errorf("cannot read %T: %w", val, err)
    57  }
    58  
    59  func errCannotWrite(val interface{}, err error) error {
    60  	return fmt.Errorf("cannot write %T: %w", val, err)
    61  }
    62  
    63  func errCannotParseString(s string, err error) error {
    64  	return fmt.Errorf("cannot parse '%v': %w", s, err)
    65  }
    66  
    67  func errValueOutOfRange(val interface{}) error {
    68  	return fmt.Errorf("value out of range: %v", val)
    69  }
    70  
    71  func errSliceIndexOutOfRange(slice bool, index int) error {
    72  	if slice {
    73  		return fmt.Errorf("slice index out of range: %v", index)
    74  	}
    75  	return fmt.Errorf("array index out of range: %v", index)
    76  }
    77  
    78  func errWrongFixedLength(expected, actual int) error {
    79  	return fmt.Errorf("expected %v bytes but got: %v", expected, actual)
    80  }
    81  
    82  func errWrongMinimumLength(expected, actual int) error {
    83  	return fmt.Errorf("expected at least %v bytes but got: %v", expected, actual)
    84  }
    85  
    86  func errWrongFixedLengths(expected1, expected2, actual int) error {
    87  	return fmt.Errorf("expected %v or %v bytes but got: %v", expected1, expected2, actual)
    88  }
    89  
    90  func errWrongContainerType(expected string, actual reflect.Type) error {
    91  	return fmt.Errorf("expected %s, got: %s", expected, actual)
    92  }
    93  
    94  func errWrongElementType(desc string, expected, actual reflect.Type) error {
    95  	return fmt.Errorf("wrong %s, expected %s, got: %v", desc, expected, actual)
    96  }
    97  
    98  func errWrongElementTypes(desc string, expected1, expected2, actual reflect.Type) error {
    99  	return fmt.Errorf("wrong %s, expected %s or %s, got: %v", desc, expected1, expected2, actual)
   100  }
   101  
   102  func errWrongDataType(desc string, expected1, expected2, actual datatype.DataType) error {
   103  	return fmt.Errorf("wrong %s, expected %s or %s, got: %v", desc, expected1, expected2, actual)
   104  }
   105  
   106  func errBytesRemaining(total int, remaining int) error {
   107  	return fmt.Errorf("source was not fully read: bytes total: %d, read: %d, remaining: %d", total, total-remaining, remaining)
   108  }
   109  
   110  func errCannotReadUdtField(i int, name string, err error) error {
   111  	return fmt.Errorf("cannot read field %d (%s): %w", i, name, err)
   112  }
   113  
   114  func errCannotDecodeUdtField(i int, name string, err error) error {
   115  	return fmt.Errorf("cannot decode field %d (%s): %w", i, name, err)
   116  }
   117  
   118  func errCannotEncodeUdtField(i int, name string, err error) error {
   119  	return fmt.Errorf("cannot encode field %d (%s): %w", i, name, err)
   120  }
   121  
   122  func errCannotEncodeElement(i int, err error) error {
   123  	return fmt.Errorf("cannot encode element %d: %w", i, err)
   124  }
   125  
   126  func errCannotEncodeMapKey(i int, err error) error {
   127  	return fmt.Errorf("cannot encode entry %d key: %w", i, err)
   128  }
   129  
   130  func errCannotEncodeMapValue(i int, err error) error {
   131  	return fmt.Errorf("cannot encode entry %d value: %w", i, err)
   132  }
   133  
   134  func errCannotDecodeElement(i int, err error) error {
   135  	return fmt.Errorf("cannot decode element %d: %w", i, err)
   136  }
   137  
   138  func errCannotDecodeMapKey(i int, err error) error {
   139  	return fmt.Errorf("cannot decode entry %d key: %w", i, err)
   140  }
   141  
   142  func errCannotDecodeMapValue(i int, err error) error {
   143  	return fmt.Errorf("cannot decode entry %d value: %w", i, err)
   144  }
   145  
   146  func errCannotReadElement(i int, err error) error {
   147  	return fmt.Errorf("cannot read element %d: %w", i, err)
   148  }
   149  
   150  func errCannotReadMapKey(i int, err error) error {
   151  	return fmt.Errorf("cannot read entry %d key: %w", i, err)
   152  }
   153  
   154  func errCannotReadMapValue(i int, err error) error {
   155  	return fmt.Errorf("cannot read entry %d value: %w", i, err)
   156  }
   157  
   158  func errCannotExtractElement(i int, err error) error {
   159  	return fmt.Errorf("cannot extract element %d: %w", i, err)
   160  }
   161  
   162  func errCannotExtractMapValue(i int, err error) error {
   163  	return fmt.Errorf("cannot extract entry %d value: %w", i, err)
   164  }
   165  
   166  func errCannotExtractUdtField(i int, name string, err error) error {
   167  	return fmt.Errorf("cannot extract field %d (%s): %w", i, name, err)
   168  }
   169  
   170  func errCannotCreateElement(i int, err error) error {
   171  	return fmt.Errorf("cannot create zero element %d: %w", i, err)
   172  }
   173  
   174  func errCannotCreateMapKey(i int, err error) error {
   175  	return fmt.Errorf("cannot create zero entry %d key: %w", i, err)
   176  }
   177  
   178  func errCannotCreateMapValue(i int, err error) error {
   179  	return fmt.Errorf("cannot create zero entry %d value: %w", i, err)
   180  }
   181  
   182  func errCannotCreateUdtField(i int, name string, err error) error {
   183  	return fmt.Errorf("cannot create zero field %d (%s): %w", i, name, err)
   184  }
   185  
   186  func errCannotInjectElement(i int, err error) error {
   187  	return fmt.Errorf("cannot inject element %d: %w", i, err)
   188  }
   189  
   190  func errCannotInjectMapEntry(i int, err error) error {
   191  	return fmt.Errorf("cannot inject entry %d: %w", i, err)
   192  }
   193  
   194  func errCannotInjectUdtField(i int, name string, err error) error {
   195  	return fmt.Errorf("cannot inject field %d (%s): %w", i, name, err)
   196  }
   197  
   198  func errDestinationUnaddressable(value reflect.Value) error {
   199  	return fmt.Errorf("destination of type %s is not addressable", value.Type())
   200  }
   201  
   202  func errStructFieldInvalid(structValue reflect.Value, key interface{}) error {
   203  	if i, ok := key.(int); ok {
   204  		return fmt.Errorf("no accessible field with index %d found in struct %s", i, structValue.Type())
   205  	} else {
   206  		return fmt.Errorf("no accessible field with name '%v' found in struct %s", key, structValue.Type())
   207  	}
   208  }
   209  
   210  func cannotWriteCollectionSize(err error) error {
   211  	return fmt.Errorf("cannot write collection size: %w", err)
   212  }
   213  
   214  func collectionSizeTooLarge(size, max int) error {
   215  	return fmt.Errorf("collection too large (%d elements, max is %d)", size, max)
   216  }
   217  
   218  func collectionSizeNegative(size int) error {
   219  	return fmt.Errorf("expected collection size >= 0, got: %d", size)
   220  }
   221  
   222  func errCannotCreateCodec(dt datatype.DataType) error {
   223  	return fmt.Errorf("cannot create data codec for CQL type %v", dt)
   224  }
   225  
   226  func errCannotFindGoType(dt datatype.DataType) error {
   227  	return fmt.Errorf("could not find any suitable Go type for CQL type %v", dt)
   228  }
   229  
   230  func errDestinationInvalid(dest interface{}) error {
   231  	if dest == nil {
   232  		return ErrNilDestination
   233  	} else if reflect.TypeOf(dest).Kind() != reflect.Ptr {
   234  		return ErrPointerTypeExpected
   235  	} else {
   236  		return ErrConversionNotSupported
   237  	}
   238  }