Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
139 lines (121 loc) · 3.74 KB
/
Copy patherrors.go
File metadata and controls
139 lines (121 loc) · 3.74 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package ant
import (
"errors"
"fmt"
"strings"
)
// Position is a location in a source file. Line and Col are 1-based; Col counts bytes.
type Position struct {
Path string
Line int
Col int
}
func (p Position) String() string {
if p.Line == 0 {
return p.Path
}
return fmt.Sprintf("%s:%d:%d", p.Path, p.Line, p.Col)
}
// Diagnostic is one error found while loading or checking a program.
type Diagnostic struct {
Pos Position
// End is where the expression the diagnostic is about ends, when it names one, and Pos is
// its start; else its Line is 0.
End Position
Message string
// Code names the rule the diagnostic reports, such as "type-mismatch" (spec/check.md
// section 3). It is empty for a failure that is not about the program, such as a prelude
// that does not load.
Code string
// Severity is "error", or "warning" for code the checker accepts that is almost always a
// mistake (spec/check.md section 4).
Severity string
// Help says how to fix it, when the message does not; Fixes are edits that would, and
// Related are other places that explain it.
Help string
Fixes []Fix
Related []Related
}
// Fix is a set of edits that removes a diagnostic. Auto marks one that is safe to apply unread
// (spec/check.md section 2, Help and fixes).
type Fix struct {
Title string
Auto bool
Edits []Edit
}
// Edit replaces the text from Start up to End, both in Start's file, with Text.
type Edit struct {
Start, End Position
Text string
}
// Related is another place that explains a diagnostic.
type Related struct {
Pos Position
Message string
}
func (d Diagnostic) Error() string {
if d.Pos.Path == "" {
return d.Message
}
return d.Pos.String() + ": " + d.Message
}
// LoadError reports why a program could not be loaded: a syntax error, a missing module, an
// import cycle, or a missing main. The ant command exits with status 2.
type LoadError struct {
Diagnostics []Diagnostic
}
func (e *LoadError) Error() string {
lines := make([]string, len(e.Diagnostics))
for i, d := range e.Diagnostics {
lines[i] = d.Error()
}
return strings.Join(lines, "\n")
}
// Frame is one entry of the stack trace a panic carries, innermost first.
type Frame struct {
Function string
Pos Position
}
// PanicError is returned by Run when a panic ended the main goroutine. The runtime has already
// written the message and trace to the run's stderr writer. The ant command exits with status 1.
type PanicError struct {
Message string
Trace []Frame
}
func (e *PanicError) Error() string { return "panic: " + e.Message }
// ResultError is returned by Run when the script returned an Err, through try or return
// (section 10). Message is the Err's error field, an Error as its message alone and any other
// error as String.from renders it, which the runtime has already written to the run's stderr
// writer, with a newline. The ant command exits with status 1.
type ResultError struct {
Message string
}
func (e *ResultError) Error() string { return e.Message }
// ExitError is returned by Run when the program called Os.exit.
type ExitError struct {
Status int
}
func (e *ExitError) Error() string { return fmt.Sprintf("exit status %d", e.Status) }
// ExitStatus maps the error returned by Load or Run to the exit status the ant command uses:
// 0 for nil, 2 for a LoadError, 1 for a PanicError or a ResultError, the status of an
// ExitError, and 1 for any other error.
func ExitStatus(err error) int {
var le *LoadError
var pe *PanicError
var ee *ExitError
var re *ResultError
switch {
case err == nil:
return 0
case errors.As(err, &le):
return 2
case errors.As(err, &pe), errors.As(err, &re):
return 1
case errors.As(err, &ee):
return ee.Status
case errors.Is(err, ErrInterrupted):
return 130
default:
return 1
}
}