-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequired.go
More file actions
23 lines (18 loc) · 749 Bytes
/
required.go
File metadata and controls
23 lines (18 loc) · 749 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package errpath
import "strings"
var _ pathWriter = (*ErrRequired)(nil)
// ErrRequired signals that a required value is missing.
type ErrRequired struct{}
// Error fulfills the error interface.
// Without a previous error path, it simply says "a value is required".
// Naturally, this is not very helpful, so it makes sense to wrap `ErrRequired` in another error such as `ErrField` so the user knows that the field was required.
func (e *ErrRequired) Error() string {
b := &strings.Builder{}
b.WriteString("a value")
e.writePath(b)
return b.String()
}
// writePath appends `" is required"` to the path so that the user knows the given path was required
func (e *ErrRequired) writePath(b *strings.Builder) {
b.WriteString(" is required")
}