File: errors.go

package info (click to toggle)
golang-github-oschwald-maxminddb-golang-v2 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,132 kB
  • sloc: perl: 557; makefile: 3
file content (56 lines) | stat: -rw-r--r-- 1,706 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Package mmdberrors is an internal package for the errors used in
// this module.
package mmdberrors

import (
	"fmt"
	"reflect"
)

// InvalidDatabaseError is returned when the database contains invalid data
// and cannot be parsed.
type InvalidDatabaseError struct {
	message string
}

// NewOffsetError creates an [InvalidDatabaseError] indicating that an offset
// pointed beyond the end of the database.
func NewOffsetError() InvalidDatabaseError {
	return InvalidDatabaseError{"unexpected end of database"}
}

// NewInvalidDatabaseError creates an [InvalidDatabaseError] using the
// provided format and format arguments.
func NewInvalidDatabaseError(format string, args ...any) InvalidDatabaseError {
	return InvalidDatabaseError{fmt.Sprintf(format, args...)}
}

func (e InvalidDatabaseError) Error() string {
	return e.message
}

// UnmarshalTypeError is returned when the value in the database cannot be
// assigned to the specified data type.
type UnmarshalTypeError struct {
	Type  reflect.Type
	Value string
}

// NewUnmarshalTypeStrError creates an [UnmarshalTypeError] when the string
// value cannot be assigned to a value of rType.
func NewUnmarshalTypeStrError(value string, rType reflect.Type) UnmarshalTypeError {
	return UnmarshalTypeError{
		Type:  rType,
		Value: value,
	}
}

// NewUnmarshalTypeError creates an [UnmarshalTypeError] when the value
// cannot be assigned to a value of rType.
func NewUnmarshalTypeError(value any, rType reflect.Type) UnmarshalTypeError {
	return NewUnmarshalTypeStrError(fmt.Sprintf("%v (%T)", value, value), rType)
}

func (e UnmarshalTypeError) Error() string {
	return fmt.Sprintf("maxminddb: cannot unmarshal %s into type %s", e.Value, e.Type)
}