Handle newer adb servers' device not found error messages.

This commit is contained in:
Zach Klippenstein 2015-09-12 15:17:16 -07:00 committed by Zach Klippenstein
parent af4b3ddcf2
commit 81043d7260
2 changed files with 65 additions and 1 deletions

View file

@ -3,6 +3,7 @@ package wire
import ( import (
"fmt" "fmt"
"io" "io"
"regexp"
"github.com/zach-klippenstein/goadb/util" "github.com/zach-klippenstein/goadb/util"
) )
@ -13,6 +14,13 @@ type ErrorResponseDetails struct {
ServerMsg string ServerMsg string
} }
// deviceNotFoundMessagePattern matches all possible error messages returned by adb servers to
// report that a matching device was not found. Used to set the util.DeviceNotFound error code on
// error values.
//
// Old servers send "device not found", and newer ones "device 'serial' not found".
var deviceNotFoundMessagePattern = regexp.MustCompile(`device( '.*')? not found`)
// Reads the status, and if failure, reads the message and returns it as an error. // Reads the status, and if failure, reads the message and returns it as an error.
// If the status is success, doesn't read the message. // If the status is success, doesn't read the message.
// req is just used to populate the AdbError, and can be nil. // req is just used to populate the AdbError, and can be nil.
@ -44,7 +52,7 @@ func adbServerError(request string, serverMsg string) error {
} }
errCode := util.AdbError errCode := util.AdbError
if serverMsg == "device not found" { if deviceNotFoundMessagePattern.MatchString(serverMsg) {
errCode = util.DeviceNotFound errCode = util.DeviceNotFound
} }

56
wire/util_test.go Normal file
View file

@ -0,0 +1,56 @@
package wire
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/zach-klippenstein/goadb/util"
)
func TestAdbServerError_NoRequest(t *testing.T) {
err := adbServerError("", "fail")
assert.Equal(t, util.Err{
Code: util.AdbError,
Message: "server error: fail",
Details: ErrorResponseDetails{
Request: "",
ServerMsg: "fail",
},
}, *(err.(*util.Err)))
}
func TestAdbServerError_WithRequest(t *testing.T) {
err := adbServerError("polite", "fail")
assert.Equal(t, util.Err{
Code: util.AdbError,
Message: "server error for polite request: fail",
Details: ErrorResponseDetails{
Request: "polite",
ServerMsg: "fail",
},
}, *(err.(*util.Err)))
}
func TestAdbServerError_DeviceNotFound(t *testing.T) {
err := adbServerError("", "device not found")
assert.Equal(t, util.Err{
Code: util.DeviceNotFound,
Message: "server error: device not found",
Details: ErrorResponseDetails{
Request: "",
ServerMsg: "device not found",
},
}, *(err.(*util.Err)))
}
func TestAdbServerError_DeviceSerialNotFound(t *testing.T) {
err := adbServerError("", "device 'LGV4801c74eccd' not found")
assert.Equal(t, util.Err{
Code: util.DeviceNotFound,
Message: "server error: device 'LGV4801c74eccd' not found",
Details: ErrorResponseDetails{
Request: "",
ServerMsg: "device 'LGV4801c74eccd' not found",
},
}, *(err.(*util.Err)))
}