...
1
2
3
4
5 package script
6
7 import (
8 "errors"
9 "fmt"
10 )
11
12
13
14 var ErrUnexpectedSuccess = errors.New("unexpected success")
15
16
17
18 type CommandError struct {
19 File string
20 Line int
21 Op string
22 Args []string
23 Err error
24 }
25
26 func cmdError(cmd *command, err error) *CommandError {
27 return &CommandError{
28 File: cmd.file,
29 Line: cmd.line,
30 Op: cmd.name,
31 Args: cmd.args,
32 Err: err,
33 }
34 }
35
36 func (e *CommandError) Error() string {
37 if len(e.Args) == 0 {
38 return fmt.Sprintf("%s:%d: %s: %v", e.File, e.Line, e.Op, e.Err)
39 }
40 return fmt.Sprintf("%s:%d: %s %s: %v", e.File, e.Line, e.Op, quoteArgs(e.Args), e.Err)
41 }
42
43 func (e *CommandError) Unwrap() error { return e.Err }
44
45
46
47
48 type UsageError struct {
49 Name string
50 Command Cmd
51 }
52
53 func (e *UsageError) Error() string {
54 usage := e.Command.Usage()
55 suffix := ""
56 if usage.Async {
57 suffix = " [&]"
58 }
59 return fmt.Sprintf("usage: %s %s%s", e.Name, usage.Args, suffix)
60 }
61
62
63
64 var ErrUsage = errors.New("invalid usage")
65
View as plain text