git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/mmdb/errors.go (about)

     1  package mmdb
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  )
     7  
     8  // InvalidDatabaseError is returned when the database contains invalid data
     9  // and cannot be parsed.
    10  type InvalidDatabaseError struct {
    11  	message string
    12  }
    13  
    14  func newOffsetError() InvalidDatabaseError {
    15  	return InvalidDatabaseError{"unexpected end of database"}
    16  }
    17  
    18  func newInvalidDatabaseError(format string, args ...any) InvalidDatabaseError {
    19  	return InvalidDatabaseError{fmt.Sprintf(format, args...)}
    20  }
    21  
    22  func (e InvalidDatabaseError) Error() string {
    23  	return e.message
    24  }
    25  
    26  // UnmarshalTypeError is returned when the value in the database cannot be
    27  // assigned to the specified data type.
    28  type UnmarshalTypeError struct {
    29  	Type  reflect.Type
    30  	Value string
    31  }
    32  
    33  func newUnmarshalTypeError(value any, rType reflect.Type) UnmarshalTypeError {
    34  	return UnmarshalTypeError{
    35  		Value: fmt.Sprintf("%v", value),
    36  		Type:  rType,
    37  	}
    38  }
    39  
    40  func (e UnmarshalTypeError) Error() string {
    41  	return fmt.Sprintf("mmdb: cannot unmarshal %s into type %s", e.Value, e.Type.String())
    42  }