Skip to content

Commit

Permalink
Gracefully handle unexpected command responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
peplin committed Aug 30, 2015
1 parent eb357df commit 0eaa615
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions library/src/main/java/com/openxc/VehicleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,20 @@ public String requestCommandMessage(CommandType type) {
VehicleMessage message = request(new Command(type));
String value = null;
if(message != null) {
CommandResponse response = message.asCommandResponse();
if(response.getStatus()) {
value = response.getMessage();
// Because we use the same key and value for commands and command
// responses, if for some reason a Command is echoed back to the
// device instead of a CommandResponse, you could get a casting
// exception when trying to cast this message here. If we got a
// Command, just ignore it and assume no response - I wasn't able to
// reproduce it but we did have a few Bugsnag reports about it.
try {
CommandResponse response = message.asCommandResponse();
if(response.getStatus()) {
value = response.getMessage();
}
} catch(ClassCastException e) {
Log.w(TAG, "Expected a command response but got " + message +
" -- ignoring, assuming no response");
}
}
return value;
Expand Down

0 comments on commit 0eaa615

Please sign in to comment.