-
Notifications
You must be signed in to change notification settings - Fork 246
/
common.go
53 lines (42 loc) · 1.55 KB
/
common.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
package spiceerrors
import "errors"
// SourcePosition is a position in the input source.
type SourcePosition struct {
// LineNumber is the 1-indexed line number in the input source.
LineNumber int
// ColumnPosition is the 1-indexed column position in the input source.
ColumnPosition int
}
// ErrorWithSource is an error that includes the source text and position
// information.
type ErrorWithSource struct {
error
// SourceCodeString is the input source code string for the error.
SourceCodeString string
// LineNumber is the (1-indexed) line number of the error, or 0 if unknown.
LineNumber uint64
// ColumnPosition is the (1-indexed) column position of the error, or 0 if
// unknown.
ColumnPosition uint64
}
// HasMetadata indicates that the error has metadata defined.
type HasMetadata interface {
// DetailsMetadata returns the metadata for details for this error.
DetailsMetadata() map[string]string
}
// Unwrap returns the inner, wrapped error.
func (err *ErrorWithSource) Unwrap() error {
return err.error
}
// NewErrorWithSource creates and returns a new ErrorWithSource.
func NewErrorWithSource(err error, sourceCodeString string, oneIndexedLineNumber uint64, oneIndexedColumnPosition uint64) *ErrorWithSource {
return &ErrorWithSource{err, sourceCodeString, oneIndexedLineNumber, oneIndexedColumnPosition}
}
// AsErrorWithSource returns the error as an ErrorWithSource, if applicable.
func AsErrorWithSource(err error) (*ErrorWithSource, bool) {
var serr *ErrorWithSource
if errors.As(err, &serr) {
return serr, true
}
return nil, false
}