-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patherrors.go
76 lines (65 loc) · 1.54 KB
/
errors.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
68
69
70
71
72
73
74
75
76
package publiccode
import (
"encoding/json"
"fmt"
"strings"
)
// A 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, args ...interface{}) ValidationError {
return ValidationError{Key: key, Description: 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 {
var s []string
for _, e := range vr {
s = append(s, e.Error())
}
return strings.Join(s, "\n")
}