/
errors.go
47 lines (40 loc) · 1002 Bytes
/
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
package jsonschema
import (
"github.com/go-faster/errors"
"github.com/ogen-go/ogen/jsonpointer"
"github.com/ogen-go/ogen/location"
)
// LocationError is a wrapper for an error that has a location.
type LocationError = location.Error
func (p *Parser) file(ctx *jsonpointer.ResolveCtx) location.File {
file := ctx.File()
if file.IsZero() {
return p.rootFile
}
return file
}
func (p *Parser) wrapField(field string, file location.File, l location.Locator, err error) error {
if err == nil || p == nil {
return err
}
return p.wrapLocation(file, l.Field(field), err)
}
func (p *Parser) wrapLocation(file location.File, l location.Locator, err error) error {
var locErr *LocationError
// Do not wrap error if it is nil or is already a LocationError.
if err == nil || p == nil || errors.As(err, &locErr) {
return err
}
pos, ok := l.Position()
if !ok {
return err
}
if file.IsZero() {
file = p.rootFile
}
return &LocationError{
File: file,
Pos: pos,
Err: err,
}
}