-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
48 lines (42 loc) · 922 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
48
package svn
import (
"fmt"
"strings"
)
// Err single svn err
type Err struct {
Code string
Message string
}
// Error svn error
type Error struct {
Cmd string
RawErrStr string
Errs []Err
RawStr string
}
// Error implement error interface
func (e Error) Error() string {
return fmt.Sprintf("exec error cmd: %s\n, err: %s stderr:\n%s", e.Cmd, e.RawErrStr, e.RawStr)
}
// HasErr check err exists by code
func (e Error) HasErr(c string) bool {
for i := range e.Errs {
if e.Errs[i].Code == c {
return true
}
}
return false
}
// NewError new svn error
func NewError(cmd, err, stderr string) Error {
lines := strings.Split(stderr, "\n")
e := Error{Cmd: cmd, RawStr: stderr, RawErrStr: err, Errs: make([]Err, 0, len(lines))}
for i := range lines {
s := strings.SplitN(lines[i], ": ", 3)
if len(s) == 3 {
e.Errs = append(e.Errs, Err{Code: s[1], Message: s[2]})
}
}
return e
}