-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
72 lines (63 loc) · 1.7 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package parse
import (
"fmt"
"strings"
"github.com/markusbkk/elvish/pkg/diag"
)
const parseErrorType = "parse error"
// Error stores multiple underlying parse errors, and can pretty print them.
type Error struct {
Entries []*diag.Error
}
var _ diag.Shower = &Error{}
// GetError returns an *Error if the given error has dynamic type *Error, i.e.
// is returned by one of the Parse functions. Otherwise it returns nil.
func GetError(e error) *Error {
if er, ok := e.(*Error); ok {
return er
}
return nil
}
func (er *Error) add(msg string, ctx *diag.Context) {
err := &diag.Error{Type: parseErrorType, Message: msg, Context: *ctx}
er.Entries = append(er.Entries, err)
}
// Error returns a string representation of the error.
func (er *Error) Error() string {
switch len(er.Entries) {
case 0:
return "no parse error"
case 1:
return er.Entries[0].Error()
default:
sb := new(strings.Builder)
// Contexts of parse error entries all have the same name
fmt.Fprintf(sb, "multiple parse errors in %s: ", er.Entries[0].Context.Name)
for i, e := range er.Entries {
if i > 0 {
fmt.Fprint(sb, "; ")
}
fmt.Fprintf(sb, "%d-%d: %s", e.Context.From, e.Context.To, e.Message)
}
return sb.String()
}
}
// Show shows the error.
func (er *Error) Show(indent string) string {
switch len(er.Entries) {
case 0:
return "no parse error"
case 1:
return er.Entries[0].Show(indent)
default:
sb := new(strings.Builder)
fmt.Fprint(sb, "Multiple parse errors:")
for _, e := range er.Entries {
sb.WriteString("\n" + indent + " ")
fmt.Fprintf(sb, "\033[31;1m%s\033[m\n", e.Message)
sb.WriteString(indent + " ")
sb.WriteString(e.Context.Show(indent + " "))
}
return sb.String()
}
}