Merge pull request #1 from zach-klippenstein/zach-klippenstein/fix-devicenotfound-newservers

Handle newer adb servers' device not found error messages.
This commit is contained in:
Zach Klippenstein 2015-09-12 15:31:49 -07:00
commit d964e00da0
2 changed files with 65 additions and 1 deletions

View file

@ -3,6 +3,7 @@ package wire
import (
"fmt"
"io"
"regexp"
"github.com/zach-klippenstein/goadb/util"
)
@ -13,6 +14,13 @@ type ErrorResponseDetails struct {
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.
// If the status is success, doesn't read the message.
// 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
if serverMsg == "device not found" {
if deviceNotFoundMessagePattern.MatchString(serverMsg) {
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)))
}