-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
111 lines (88 loc) · 2.79 KB
/
errors.go
File metadata and controls
111 lines (88 loc) · 2.79 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
package lvmclient
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
)
const (
errMethodNotFound = `Method "GetAll" with signature "s" on interface "org.freedesktop.DBus.Properties" doesn't exist` + "\n"
)
var (
ErrInvalidParams = errors.New("invalid params")
)
var (
ErrInsufficientFreeSpace = errors.New("insufficient free space")
ErrLogicalVolumeSameSize = errors.New("logical_volume: already has this size")
ErrLogicalVolumeNotFound = errors.New("logical_volume: not found")
ErrLogicalVolumeAlreadyExist = errors.New("logical_volume: already exist")
)
var (
ErrVolumeGroupNotFound = errors.New("volume_group: not found")
)
// --
type JobError struct {
Code int32
Message string
}
func (je *JobError) Error() string {
return fmt.Sprintf("job complete with error. code:%d, message:%s", je.Code, je.Message)
}
var lvmErrorRegexp = regexp.MustCompile(`\('(.*)', 'Exit code ([0-9]*), stderr =[ *](.*)'\)`)
func IsLvmError(err error) (*LvmError, bool) {
if err == nil {
return nil, false
}
je, ok := err.(*JobError)
if !ok {
return nil, false
}
if je.Code == -1 {
matches := lvmErrorRegexp.FindAllStringSubmatch(je.Message, -1)
if matches == nil {
return nil, false
}
code := matches[0][2]
description := matches[0][3]
lvmCode, err := strconv.ParseInt(code, 10, 64)
if err != nil {
return nil, false
}
return &LvmError{
Code: int32(lvmCode),
Description: strings.TrimSpace(description),
}, true
}
return nil, false
}
var (
lvmVolumeGroupInsufficientFreeSpace = regexp.MustCompile(`Volume group "(.*)" has insufficient free space \(([0-9]*) extents\): (.*) required\.`)
lvmInsufficientFreeSpace = regexp.MustCompile(`Insufficient free space: (.*) extents needed, but only (.*) available`)
lvmLogicalVolumeAlreadyExist = regexp.MustCompile(`Logical Volume "(.*)" already exists in volume group "(.*)"`)
lvmLogicalVolumeSameSize = regexp.MustCompile(`^New size \((.*) extents\) matches existing size \((.*) extents\)`)
)
type LvmError struct {
Code int32
Description string
}
func (lerr *LvmError) ToError() error {
switch lerr.Code {
case 5:
if matches := lvmLogicalVolumeAlreadyExist.FindAllStringSubmatch(lerr.Description, -1); matches != nil {
return ErrLogicalVolumeAlreadyExist
}
if matches := lvmLogicalVolumeSameSize.FindAllStringSubmatch(lerr.Description, -1); matches != nil {
return ErrLogicalVolumeSameSize
}
if matches := lvmInsufficientFreeSpace.FindAllStringSubmatch(lerr.Description, -1); matches != nil {
return ErrInsufficientFreeSpace
}
if matches := lvmVolumeGroupInsufficientFreeSpace.FindAllStringSubmatch(lerr.Description, -1); matches != nil {
return ErrInsufficientFreeSpace
}
fallthrough
default:
return fmt.Errorf("%d - %s", lerr.Code, lerr.Description)
}
}