-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
67 lines (55 loc) · 2.57 KB
/
types.go
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
57
58
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: LGPL-3.0-only
package errors
import (
"strconv"
strconv_ "github.com/cobratbq/goutils/std/strconv"
)
// StringError as a base type for const errors.
//
// This type is intended to be used as replacement for errors.New(..) from std, such that you can
// define an error as const (constant). The idea being that the "root" error type is just the type
// and the circumstances within which the error occurs are dictated by any number of contexts
// wrapped around the root error.
type StringError struct{ v string }
// NewStringError creates a new string-based error instance. If used as-is, the pointer that is
// returned is uniquely-identifying and therefore immediately useable. In case the StringError is a
// basis for a custom error type, the pointer can be dereferenced to include the value itself in the
// newly defined struct-type, for efficiency.
func NewStringError(msg string) *StringError {
return &StringError{msg}
}
func (e *StringError) Error() string {
return string(e.v)
}
// UintError as a base type for const errors.
//
// Similar to StringError, this type can be used to declare const errors. This type is based on uint
// therefore is most suitable for errors that are signaled through a numeric code, such as with
// HTTP-like protocols.
type UintError struct{ v uint }
// NewUintError creates a new uint-based error instance. If used as-is, the pointer that is returned
// is uniquely-identifying and therefore immediately useable. In case UintError is a basis for a
// custom error type, the pointer can be dereferenced as to include the value itself in the newly
// defined struct-type, for efficiency.
func NewUintError(value uint) *UintError {
return &UintError{value}
}
func (e *UintError) Error() string {
return strconv.FormatUint(uint64(e.v), strconv_.DecimalBase)
}
// IntError as a base type for const errors.
//
// Similar to StringError, this type can be used to declare const errors. This type is based on int,
// therefore most suitable for errors that are signaled through a numeric code, such as with
// HTTP-like protocols.
type IntError struct{ v int }
// NewIntError creates a new int-based error instance. If used as-is, the pointer that is returned
// is uniquely-identifying and therefore immediately useable. In case the IntError is a basis for a
// custom error type, the pointer can be dereferenced to include the value itself in the newly
// defined struct-type, for efficiency.
func NewIntError(v int) *IntError {
return &IntError{v}
}
func (e *IntError) Error() string {
return strconv.FormatInt(int64(e.v), strconv_.DecimalBase)
}