make libvirt error types private

also change ErrIsNotFound to IsNotFound

Signed-off-by: Penghui Liao <liaoishere@gmail.com>
This commit is contained in:
Penghui Liao 2018-03-23 10:59:12 +08:00
parent 482640a285
commit 2d4c9f9f6d
No known key found for this signature in database
GPG Key ID: B847939A95E9B47A
4 changed files with 192 additions and 193 deletions

View File

@ -1964,192 +1964,192 @@ const (
StreamEventHangup StreamEventType = 8 StreamEventHangup StreamEventType = 8
) )
// ErrorLevel as declared in libvirt/virterror.h:44 // errorLevel as declared in libvirt/virterror.h:44
type ErrorLevel int32 type errorLevel int32
// ErrorLevel enumeration from libvirt/virterror.h:44 // errorLevel enumeration from libvirt/virterror.h:44
const ( const (
ErrNone ErrorLevel = iota errNone errorLevel = iota
ErrWarning ErrorLevel = 1 errWarning errorLevel = 1
ErrError ErrorLevel = 2 errError errorLevel = 2
) )
// ErrorDomain as declared in libvirt/virterror.h:139 // errorDomain as declared in libvirt/virterror.h:139
type ErrorDomain int32 type errorDomain int32
// ErrorDomain enumeration from libvirt/virterror.h:139 // errorDomain enumeration from libvirt/virterror.h:139
const ( const (
FromNone ErrorDomain = iota fromNone errorDomain = iota
FromXen ErrorDomain = 1 fromXen errorDomain = 1
FromXend ErrorDomain = 2 fromXend errorDomain = 2
FromXenstore ErrorDomain = 3 fromXenstore errorDomain = 3
FromSexpr ErrorDomain = 4 fromSexpr errorDomain = 4
FromXML ErrorDomain = 5 fromXML errorDomain = 5
FromDom ErrorDomain = 6 fromDom errorDomain = 6
FromRPC ErrorDomain = 7 fromRPC errorDomain = 7
FromProxy ErrorDomain = 8 fromProxy errorDomain = 8
FromConf ErrorDomain = 9 fromConf errorDomain = 9
FromQemu ErrorDomain = 10 fromQemu errorDomain = 10
FromNet ErrorDomain = 11 fromNet errorDomain = 11
FromTest ErrorDomain = 12 fromTest errorDomain = 12
FromRemote ErrorDomain = 13 fromRemote errorDomain = 13
FromOpenvz ErrorDomain = 14 fromOpenvz errorDomain = 14
FromXenxm ErrorDomain = 15 fromXenxm errorDomain = 15
FromStatsLinux ErrorDomain = 16 fromStatsLinux errorDomain = 16
FromLxc ErrorDomain = 17 fromLxc errorDomain = 17
FromStorage ErrorDomain = 18 fromStorage errorDomain = 18
FromNetwork ErrorDomain = 19 fromNetwork errorDomain = 19
FromDomain ErrorDomain = 20 fromDomain errorDomain = 20
FromUml ErrorDomain = 21 fromUml errorDomain = 21
FromNodedev ErrorDomain = 22 fromNodedev errorDomain = 22
FromXenInotify ErrorDomain = 23 fromXenInotify errorDomain = 23
FromSecurity ErrorDomain = 24 fromSecurity errorDomain = 24
FromVbox ErrorDomain = 25 fromVbox errorDomain = 25
FromInterface ErrorDomain = 26 fromInterface errorDomain = 26
FromOne ErrorDomain = 27 fromOne errorDomain = 27
FromEsx ErrorDomain = 28 fromEsx errorDomain = 28
FromPhyp ErrorDomain = 29 fromPhyp errorDomain = 29
FromSecret ErrorDomain = 30 fromSecret errorDomain = 30
FromCPU ErrorDomain = 31 fromCPU errorDomain = 31
FromXenapi ErrorDomain = 32 fromXenapi errorDomain = 32
FromNwfilter ErrorDomain = 33 fromNwfilter errorDomain = 33
FromHook ErrorDomain = 34 fromHook errorDomain = 34
FromDomainSnapshot ErrorDomain = 35 fromDomainSnapshot errorDomain = 35
FromAudit ErrorDomain = 36 fromAudit errorDomain = 36
FromSysinfo ErrorDomain = 37 fromSysinfo errorDomain = 37
FromStreams ErrorDomain = 38 fromStreams errorDomain = 38
FromVmware ErrorDomain = 39 fromVmware errorDomain = 39
FromEvent ErrorDomain = 40 fromEvent errorDomain = 40
FromLibxl ErrorDomain = 41 fromLibxl errorDomain = 41
FromLocking ErrorDomain = 42 fromLocking errorDomain = 42
FromHyperv ErrorDomain = 43 fromHyperv errorDomain = 43
FromCapabilities ErrorDomain = 44 fromCapabilities errorDomain = 44
FromURI ErrorDomain = 45 fromURI errorDomain = 45
FromAuth ErrorDomain = 46 fromAuth errorDomain = 46
FromDbus ErrorDomain = 47 fromDbus errorDomain = 47
FromParallels ErrorDomain = 48 fromParallels errorDomain = 48
FromDevice ErrorDomain = 49 fromDevice errorDomain = 49
FromSSH ErrorDomain = 50 fromSSH errorDomain = 50
FromLockspace ErrorDomain = 51 fromLockspace errorDomain = 51
FromInitctl ErrorDomain = 52 fromInitctl errorDomain = 52
FromIdentity ErrorDomain = 53 fromIdentity errorDomain = 53
FromCgroup ErrorDomain = 54 fromCgroup errorDomain = 54
FromAccess ErrorDomain = 55 fromAccess errorDomain = 55
FromSystemd ErrorDomain = 56 fromSystemd errorDomain = 56
FromBhyve ErrorDomain = 57 fromBhyve errorDomain = 57
FromCrypto ErrorDomain = 58 fromCrypto errorDomain = 58
FromFirewall ErrorDomain = 59 fromFirewall errorDomain = 59
FromPolkit ErrorDomain = 60 fromPolkit errorDomain = 60
FromThread ErrorDomain = 61 fromThread errorDomain = 61
FromAdmin ErrorDomain = 62 fromAdmin errorDomain = 62
FromLogging ErrorDomain = 63 fromLogging errorDomain = 63
FromXenxl ErrorDomain = 64 fromXenxl errorDomain = 64
FromPerf ErrorDomain = 65 fromPerf errorDomain = 65
FromLibssh ErrorDomain = 66 fromLibssh errorDomain = 66
) )
// ErrorNumber as declared in libvirt/virterror.h:322 // errorNumber as declared in libvirt/virterror.h:322
type ErrorNumber int32 type errorNumber int32
// ErrorNumber enumeration from libvirt/virterror.h:322 // errorNumber enumeration from libvirt/virterror.h:322
const ( const (
ErrOk ErrorNumber = iota errOk errorNumber = iota
ErrInternalError ErrorNumber = 1 errInternalError errorNumber = 1
ErrNoMemory ErrorNumber = 2 errNoMemory errorNumber = 2
ErrNoSupport ErrorNumber = 3 errNoSupport errorNumber = 3
ErrUnknownHost ErrorNumber = 4 errUnknownHost errorNumber = 4
ErrNoConnect ErrorNumber = 5 errNoConnect errorNumber = 5
ErrInvalidConn ErrorNumber = 6 errInvalidConn errorNumber = 6
ErrInvalidDomain ErrorNumber = 7 errInvalidDomain errorNumber = 7
ErrInvalidArg ErrorNumber = 8 errInvalidArg errorNumber = 8
ErrOperationFailed ErrorNumber = 9 errOperationFailed errorNumber = 9
ErrGetFailed ErrorNumber = 10 errGetFailed errorNumber = 10
ErrPostFailed ErrorNumber = 11 errPostFailed errorNumber = 11
ErrHTTPError ErrorNumber = 12 errHTTPError errorNumber = 12
ErrSexprSerial ErrorNumber = 13 errSexprSerial errorNumber = 13
ErrNoXen ErrorNumber = 14 errNoXen errorNumber = 14
ErrXenCall ErrorNumber = 15 errXenCall errorNumber = 15
ErrOsType ErrorNumber = 16 errOsType errorNumber = 16
ErrNoKernel ErrorNumber = 17 errNoKernel errorNumber = 17
ErrNoRoot ErrorNumber = 18 errNoRoot errorNumber = 18
ErrNoSource ErrorNumber = 19 errNoSource errorNumber = 19
ErrNoTarget ErrorNumber = 20 errNoTarget errorNumber = 20
ErrNoName ErrorNumber = 21 errNoName errorNumber = 21
ErrNoOs ErrorNumber = 22 errNoOs errorNumber = 22
ErrNoDevice ErrorNumber = 23 errNoDevice errorNumber = 23
ErrNoXenstore ErrorNumber = 24 errNoXenstore errorNumber = 24
ErrDriverFull ErrorNumber = 25 errDriverFull errorNumber = 25
ErrCallFailed ErrorNumber = 26 errCallFailed errorNumber = 26
ErrXMLError ErrorNumber = 27 errXMLError errorNumber = 27
ErrDomExist ErrorNumber = 28 errDomExist errorNumber = 28
ErrOperationDenied ErrorNumber = 29 errOperationDenied errorNumber = 29
ErrOpenFailed ErrorNumber = 30 errOpenFailed errorNumber = 30
ErrReadFailed ErrorNumber = 31 errReadFailed errorNumber = 31
ErrParseFailed ErrorNumber = 32 errParseFailed errorNumber = 32
ErrConfSyntax ErrorNumber = 33 errConfSyntax errorNumber = 33
ErrWriteFailed ErrorNumber = 34 errWriteFailed errorNumber = 34
ErrXMLDetail ErrorNumber = 35 errXMLDetail errorNumber = 35
ErrInvalidNetwork ErrorNumber = 36 errInvalidNetwork errorNumber = 36
ErrNetworkExist ErrorNumber = 37 errNetworkExist errorNumber = 37
ErrSystemError ErrorNumber = 38 errSystemError errorNumber = 38
ErrRPC ErrorNumber = 39 errRPC errorNumber = 39
ErrGnutlsError ErrorNumber = 40 errGnutlsError errorNumber = 40
WarNoNetwork ErrorNumber = 41 warNoNetwork errorNumber = 41
ErrNoDomain ErrorNumber = 42 errNoDomain errorNumber = 42
ErrNoNetwork ErrorNumber = 43 errNoNetwork errorNumber = 43
ErrInvalidMac ErrorNumber = 44 errInvalidMac errorNumber = 44
ErrAuthFailed ErrorNumber = 45 errAuthFailed errorNumber = 45
ErrInvalidStoragePool ErrorNumber = 46 errInvalidStoragePool errorNumber = 46
ErrInvalidStorageVol ErrorNumber = 47 errInvalidStorageVol errorNumber = 47
WarNoStorage ErrorNumber = 48 warNoStorage errorNumber = 48
ErrNoStoragePool ErrorNumber = 49 errNoStoragePool errorNumber = 49
ErrNoStorageVol ErrorNumber = 50 errNoStorageVol errorNumber = 50
WarNoNode ErrorNumber = 51 warNoNode errorNumber = 51
ErrInvalidNodeDevice ErrorNumber = 52 errInvalidNodeDevice errorNumber = 52
ErrNoNodeDevice ErrorNumber = 53 errNoNodeDevice errorNumber = 53
ErrNoSecurityModel ErrorNumber = 54 errNoSecurityModel errorNumber = 54
ErrOperationInvalid ErrorNumber = 55 errOperationInvalid errorNumber = 55
WarNoInterface ErrorNumber = 56 warNoInterface errorNumber = 56
ErrNoInterface ErrorNumber = 57 errNoInterface errorNumber = 57
ErrInvalidInterface ErrorNumber = 58 errInvalidInterface errorNumber = 58
ErrMultipleInterfaces ErrorNumber = 59 errMultipleInterfaces errorNumber = 59
WarNoNwfilter ErrorNumber = 60 warNoNwfilter errorNumber = 60
ErrInvalidNwfilter ErrorNumber = 61 errInvalidNwfilter errorNumber = 61
ErrNoNwfilter ErrorNumber = 62 errNoNwfilter errorNumber = 62
ErrBuildFirewall ErrorNumber = 63 errBuildFirewall errorNumber = 63
WarNoSecret ErrorNumber = 64 warNoSecret errorNumber = 64
ErrInvalidSecret ErrorNumber = 65 errInvalidSecret errorNumber = 65
ErrNoSecret ErrorNumber = 66 errNoSecret errorNumber = 66
ErrConfigUnsupported ErrorNumber = 67 errConfigUnsupported errorNumber = 67
ErrOperationTimeout ErrorNumber = 68 errOperationTimeout errorNumber = 68
ErrMigratePersistFailed ErrorNumber = 69 errMigratePersistFailed errorNumber = 69
ErrHookScriptFailed ErrorNumber = 70 errHookScriptFailed errorNumber = 70
ErrInvalidDomainSnapshot ErrorNumber = 71 errInvalidDomainSnapshot errorNumber = 71
ErrNoDomainSnapshot ErrorNumber = 72 errNoDomainSnapshot errorNumber = 72
ErrInvalidStream ErrorNumber = 73 errInvalidStream errorNumber = 73
ErrArgumentUnsupported ErrorNumber = 74 errArgumentUnsupported errorNumber = 74
ErrStorageProbeFailed ErrorNumber = 75 errStorageProbeFailed errorNumber = 75
ErrStoragePoolBuilt ErrorNumber = 76 errStoragePoolBuilt errorNumber = 76
ErrSnapshotRevertRisky ErrorNumber = 77 errSnapshotRevertRisky errorNumber = 77
ErrOperationAborted ErrorNumber = 78 errOperationAborted errorNumber = 78
ErrAuthCancelled ErrorNumber = 79 errAuthCancelled errorNumber = 79
ErrNoDomainMetadata ErrorNumber = 80 errNoDomainMetadata errorNumber = 80
ErrMigrateUnsafe ErrorNumber = 81 errMigrateUnsafe errorNumber = 81
ErrOverflow ErrorNumber = 82 errOverflow errorNumber = 82
ErrBlockCopyActive ErrorNumber = 83 errBlockCopyActive errorNumber = 83
ErrOperationUnsupported ErrorNumber = 84 errOperationUnsupported errorNumber = 84
ErrSSH ErrorNumber = 85 errSSH errorNumber = 85
ErrAgentUnresponsive ErrorNumber = 86 errAgentUnresponsive errorNumber = 86
ErrResourceBusy ErrorNumber = 87 errResourceBusy errorNumber = 87
ErrAccessDenied ErrorNumber = 88 errAccessDenied errorNumber = 88
ErrDbusService ErrorNumber = 89 errDbusService errorNumber = 89
ErrStorageVolExist ErrorNumber = 90 errStorageVolExist errorNumber = 90
ErrCPUIncompatible ErrorNumber = 91 errCPUIncompatible errorNumber = 91
ErrXMLInvalidSchema ErrorNumber = 92 errXMLInvalidSchema errorNumber = 92
ErrMigrateFinishOk ErrorNumber = 93 errMigrateFinishOk errorNumber = 93
ErrAuthUnavailable ErrorNumber = 94 errAuthUnavailable errorNumber = 94
ErrNoServer ErrorNumber = 95 errNoServer errorNumber = 95
ErrNoClient ErrorNumber = 96 errNoClient errorNumber = 96
ErrAgentUnsynced ErrorNumber = 97 errAgentUnsynced errorNumber = 97
ErrLibssh ErrorNumber = 98 errLibssh errorNumber = 98
) )

View File

@ -56,6 +56,7 @@ TRANSLATOR:
- {action: replace, from: "Rpc([A-Z]|$)", to: "RPC$1"} - {action: replace, from: "Rpc([A-Z]|$)", to: "RPC$1"}
- {action: replace, from: "Ssh([A-Z]|$)", to: "SSH$1"} - {action: replace, from: "Ssh([A-Z]|$)", to: "SSH$1"}
- {action: replace, from: "Http([A-Z]|$)", to: "HTTP$1"} - {action: replace, from: "Http([A-Z]|$)", to: "HTTP$1"}
- {transform: unexport, from: "^(Err|From|War)"}
const: const:
- {action: accept, from: "^VIR_"} - {action: accept, from: "^VIR_"}
# Special case to prevent a collision with a type: # Special case to prevent a collision with a type:

20
rpc.go
View File

@ -18,7 +18,6 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt"
"io" "io"
"strings" "strings"
"sync/atomic" "sync/atomic"
@ -115,10 +114,10 @@ type libvirtError struct {
} }
func (e libvirtError) Error() string { func (e libvirtError) Error() string {
return fmt.Sprintf("%s, code: %d, domain id: %d", e.Message, e.Code, e.DomainID) return e.Message
} }
func checkError(err error, expectedError ErrorNumber) bool { func checkError(err error, expectedError errorNumber) bool {
e, ok := err.(libvirtError) e, ok := err.(libvirtError)
if ok { if ok {
return e.Code == uint32(expectedError) return e.Code == uint32(expectedError)
@ -126,14 +125,9 @@ func checkError(err error, expectedError ErrorNumber) bool {
return false return false
} }
// ErrIsNotFound detects libvirt's ERR_NO_DOMAIN. // IsNotFound detects libvirt's ERR_NO_DOMAIN.
func ErrIsNotFound(err error) bool { func IsNotFound(err error) bool {
return checkError(err, ErrNoDomain) return checkError(err, errNoDomain)
}
// ErrIsOK detects libvirt's ERR_OK.
func ErrIsOK(err error) bool {
return checkError(err, ErrOk)
} }
// listen processes incoming data and routes // listen processes incoming data and routes
@ -351,6 +345,10 @@ func decodeError(buf []byte) error {
if strings.Contains(e.Message, "unknown procedure") { if strings.Contains(e.Message, "unknown procedure") {
return ErrUnsupported return ErrUnsupported
} }
// if libvirt returns ERR_OK, ignore the error
if checkError(e, errOk) {
return nil
}
return e return e
} }

View File

@ -89,7 +89,7 @@ var (
} }
testErrorMessage = []byte{ testErrorMessage = []byte{
0x00, 0x00, 0x00, 0x37, // code (55, ErrOperationInvalid) 0x00, 0x00, 0x00, 0x37, // code (55, errOperationInvalid)
0x00, 0x00, 0x00, 0x0a, // domain id 0x00, 0x00, 0x00, 0x0a, // domain id
// message ("Requested operation is not valid: domain is not running") // message ("Requested operation is not valid: domain is not running")
@ -107,7 +107,7 @@ var (
} }
testErrorNotFoundMessage = []byte{ testErrorNotFoundMessage = []byte{
0x00, 0x00, 0x00, 0x2a, // code (42 ErrDoDmain) 0x00, 0x00, 0x00, 0x2a, // code (42 errDoDmain)
0x00, 0x00, 0x00, 0x0a, // domain id 0x00, 0x00, 0x00, 0x0a, // domain id
// message // message
@ -224,7 +224,7 @@ func TestDecodeEvent(t *testing.T) {
func TestDecodeError(t *testing.T) { func TestDecodeError(t *testing.T) {
expectedMsg := "Requested operation is not valid: domain is not running" expectedMsg := "Requested operation is not valid: domain is not running"
expectedCode := ErrOperationInvalid expectedCode := errOperationInvalid
err := decodeError(testErrorMessage) err := decodeError(testErrorMessage)
e := err.(libvirtError) e := err.(libvirtError)
@ -238,7 +238,7 @@ func TestDecodeError(t *testing.T) {
func TestErrNotFound(t *testing.T) { func TestErrNotFound(t *testing.T) {
err := decodeError(testErrorNotFoundMessage) err := decodeError(testErrorNotFoundMessage)
ok := ErrIsNotFound(err) ok := IsNotFound(err)
if !ok { if !ok {
t.Errorf("expected true, got %t", ok) t.Errorf("expected true, got %t", ok)
} }