-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
48 lines (40 loc) · 1 KB
/
error.go
File metadata and controls
48 lines (40 loc) · 1 KB
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
package httpmatter
import (
"fmt"
"strings"
)
var ErrOpeningFile = newErrFn("failed to open file")
var ErrReadingFile = newErrFn("failed to read file")
var ErrParsingFile = newErrFn("failed to parse file")
var ErrParsingTemplate = newErrFn("failed to parse template")
var ErrExecutingTemplate = newErrFn("failed to execute template")
var ErrCreatingMatter = newErrFn("failed to create matter")
var ErrNotImplemented = newErrFn("not implemented")
type err struct {
message string
data map[string]any
}
func newErrFn(message string) func() *err {
return func() *err {
return &err{
message: message,
}
}
}
func (e *err) WithData(key string, value any) *err {
if e.data == nil {
e.data = make(map[string]any)
}
e.data[key] = value
return e
}
func (e *err) Is(err error) bool {
return strings.Contains(err.Error(), e.message+":")
}
func (e *err) WithError(err error) *err {
e.WithData("error", err.Error())
return e
}
func (e *err) Error() string {
return fmt.Sprintf("%s: %+v", e.message, e.data)
}