-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
52 lines (45 loc) · 1.45 KB
/
Copy patherrors.go
File metadata and controls
52 lines (45 loc) · 1.45 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
package codemeta
import (
"errors"
"fmt"
)
var (
ErrSyntax = errors.New("invalid JSON")
ErrUnsupported = errors.New("unsupported syntax")
ErrType = errors.New("unrepresentable document")
ErrLimit = errors.New("resource limit exceeded")
ErrOptions = errors.New("invalid parse options")
ErrIO = errors.New("input/output failure")
)
// Position identifies a one-based Unicode character line and column.
// Zero means no source position is available.
type Position struct {
Line int `json:"line,omitempty"`
Column int `json:"column,omitempty"`
}
type Diagnostic struct {
Code string `json:"code"`
Path string `json:"path,omitempty"`
Message string `json:"message"`
Position
}
// Error retains a machine-readable category and the original I/O error, if any.
type Error struct {
Diagnostic
Category error
Cause error
}
func (e *Error) Error() string {
if e.Line != 0 {
return fmt.Sprintf("codemeta: %d:%d: %s", e.Line, e.Column, e.Message)
}
return "codemeta: " + e.Message
}
func (e *Error) Is(target error) bool { return target == e.Category }
func (e *Error) Unwrap() error { return e.Cause }
func failure(category error, code, message string, pos Position) error {
return &Error{Diagnostic: Diagnostic{Code: code, Message: message, Position: pos}, Category: category}
}
func ioFailure(err error) error {
return &Error{Diagnostic: Diagnostic{Code: "io", Message: err.Error()}, Category: ErrIO, Cause: err}
}