-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_error.go
76 lines (58 loc) · 1.29 KB
/
cmd_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
73
74
75
76
package hype
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"
"github.com/markbates/clam"
)
type CmdError struct {
clam.RunError
Filename string
}
func (ce CmdError) MarshalJSON() ([]byte, error) {
mm := map[string]any{
"args": ce.Args,
"error": errForJSON(ce.Err),
"exit": ce.Exit,
"filename": ce.Filename,
"output": string(ce.Output),
"root": ce.Dir,
"type": toType(ce),
}
return json.MarshalIndent(mm, "", " ")
}
func (ce CmdError) Error() string {
sb := &strings.Builder{}
var lines []string
fp := filepath.Join(ce.Dir, ce.Filename)
if len(fp) > 0 {
lines = append(lines, fmt.Sprintf("filepath: %s", fp))
}
if len(ce.Args) > 0 {
lines = append(lines, fmt.Sprintf("cmd: $ %s", strings.Join(ce.Args, " ")))
}
if ce.Exit != 0 {
lines = append(lines, fmt.Sprintf("exit: %d", ce.Exit))
}
if ce.Err != nil {
lines = append(lines, fmt.Sprintf("cmd error: %s", ce.Err))
}
sb.WriteString(strings.Join(lines, "\n"))
s := sb.String()
return strings.TrimSpace(s)
}
func (ce CmdError) As(target any) bool {
ex, ok := target.(*CmdError)
if !ok {
return ce.RunError.As(target)
}
(*ex) = ce
return true
}
func (ce CmdError) Is(target error) bool {
if _, ok := target.(CmdError); ok {
return true
}
return ce.RunError.Is(target)
}