errors.go 1.1 KB

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