forked from italia/publiccode-parser-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
88 lines (68 loc) · 1.69 KB
/
errors.go
File metadata and controls
88 lines (68 loc) · 1.69 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package publiccode
import (
"encoding/json"
"fmt"
"strings"
)
// ParseError is generic parse error.
type ParseError struct {
Reason string
}
func (e ParseError) Error() string {
return e.Reason
}
type ValidationError struct {
Key string `json:"key"`
Description string `json:"description"`
Line int `json:"line"`
Column int `json:"column"`
}
func (e ValidationError) Error() string {
key := ""
if e.Key != "" {
key = fmt.Sprintf("%s: ", e.Key)
}
return fmt.Sprintf("publiccode.yml:%d:%d: error: %s%s", e.Line, e.Column, key, e.Description)
}
func (e ValidationError) MarshalJSON() ([]byte, error) {
type Ve ValidationError
return json.Marshal(&struct {
*Ve
Type string `json:"type"`
}{
Ve: (*Ve)(&e),
Type: "error",
})
}
func newValidationError(key string, description string) ValidationError {
return ValidationError{Key: key, Description: description}
}
func newValidationErrorf(key string, description string, args ...any) ValidationError {
return newValidationError(key, fmt.Sprintf(description, args...))
}
type ValidationWarning ValidationError
func (e ValidationWarning) Error() string {
key := ""
if e.Key != "" {
key = fmt.Sprintf("%s: ", e.Key)
}
return fmt.Sprintf("publiccode.yml:%d:%d: warning: %s%s", e.Line, e.Column, key, e.Description)
}
func (e ValidationWarning) MarshalJSON() ([]byte, error) {
type Ve ValidationError
return json.Marshal(&struct {
*Ve
Type string `json:"type"`
}{
Ve: (*Ve)(&e),
Type: "warning",
})
}
type ValidationResults []error
func (vr ValidationResults) Error() string {
s := make([]string, 0, len(vr))
for _, e := range vr {
s = append(s, e.Error())
}
return strings.Join(s, "\n")
}