-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
47 lines (40 loc) · 963 Bytes
/
error.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 generator
import (
"bytes"
"fmt"
)
// ParseError provides an error implementation which optionally includes
// source filename and line or row number information. It can encapsulate
// another error, or be built with an error string.
type ParseError struct {
Filename string
Line, Row int
Err error
ErrStr string
}
func (e *ParseError) Error() string {
var b bytes.Buffer
// File location can be either row (for CSV files) or line. Both are
// optional.
if e.Row > 0 {
b.WriteString(fmt.Sprintf("row %d", e.Row))
} else if e.Line > 0 {
b.WriteString(fmt.Sprintf("line %d", e.Line))
}
if b.Len() > 0 && e.Filename != "" {
b.WriteString(" of ")
}
b.WriteString(e.Filename)
// Error can be nested, or a simple string.
var errStr string
if e.Err != nil {
errStr = e.Err.Error()
} else {
errStr = e.ErrStr
}
if b.Len() > 0 && errStr != "" {
b.WriteString(": ")
}
b.WriteString(errStr)
return b.String()
}