@@ -16,24 +16,43 @@ var _ interface {
16
16
17
17
// LocationError is a wrapper for an error that has a location.
18
18
type LocationError struct {
19
- loc ogenjson.Location
20
- err error
19
+ file string
20
+ loc ogenjson.Location
21
+ err error
21
22
}
22
23
23
24
// Unwrap implements errors.Wrapper.
24
25
func (e * LocationError ) Unwrap () error {
25
26
return e .err
26
27
}
27
28
29
+ func (e * LocationError ) fileName () string {
30
+ filename := e .file
31
+ if filename != "" {
32
+ switch {
33
+ case e .loc .Line != 0 :
34
+ // Line is set, so return "${filename}:".
35
+ filename += ":"
36
+ case e .loc .JSONPointer != "" :
37
+ // Line is not set, but JSONPointer is set, so return "${filename}#${JSONPointer}".
38
+ filename += "#"
39
+ default :
40
+ // Neither line nor JSONPointer is set, so return empty string.
41
+ return ""
42
+ }
43
+ }
44
+ return filename
45
+ }
46
+
28
47
// FormatError implements errors.Formatter.
29
48
func (e * LocationError ) FormatError (p errors.Printer ) (next error ) {
30
- p .Printf ("at %s" , e .loc )
49
+ p .Printf ("at %s%s" , e . fileName () , e .loc )
31
50
return e .err
32
51
}
33
52
34
53
// Error implements error.
35
54
func (e * LocationError ) Error () string {
36
- return fmt .Sprintf ("at %s: %s" , e .loc , e .err )
55
+ return fmt .Sprintf ("at %s%s : %s" , e . fileName () , e .loc , e .err )
37
56
}
38
57
39
58
func (p * parser ) wrapLocation (l ogenjson.Locatable , err error ) error {
@@ -45,7 +64,8 @@ func (p *parser) wrapLocation(l ogenjson.Locatable, err error) error {
45
64
return err
46
65
}
47
66
return & LocationError {
48
- loc : loc ,
49
- err : err ,
67
+ file : p .filename ,
68
+ loc : loc ,
69
+ err : err ,
50
70
}
51
71
}
0 commit comments