-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
62 lines (49 loc) · 1.17 KB
/
errors.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
package scpi
import (
"fmt"
"regexp"
"strconv"
"strings"
)
// InvalidProtocolError occures if the protocol is invalid.
type InvalidProtocolError string
func (e InvalidProtocolError) Error() string {
return fmt.Sprintf("invalid protocol %s", string(e))
}
// InvalidFormatError occures if the format of the response is invalid.
type InvalidFormatError string
func (e InvalidFormatError) Error() string {
return fmt.Sprintf("invalid format: %s", string(e))
}
// CommandError is the error of SCPI commands.
type CommandError struct {
cmd string
code int
msg string
}
// Code returns the error code of a SCPI device.
func (e *CommandError) Code() int {
return e.code
}
func (e *CommandError) Error() string {
return fmt.Sprintf("'%s' returned %d: %s", e.cmd, e.code, e.msg)
}
var cmdErrRegexp = regexp.MustCompile(`([+-]\d+),\"(.*?)\"`)
func confirmError(cmd, errRes string) error {
re := cmdErrRegexp.Copy()
g := re.FindStringSubmatch(errRes)
if g == nil {
return InvalidFormatError(errRes)
}
code, _ := strconv.Atoi(g[1])
if code == 0 {
return nil
}
msg := strings.ToLower(g[2])
cmdErr := &CommandError{
cmd: cmd,
code: code,
msg: msg,
}
return cmdErr
}