Skip to content

Commit 287cde5

Browse files
committed
feat: error handling OK
1 parent a6ace11 commit 287cde5

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

examples/rpc_lite_client/rpc_lite_client.ino

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ void blink_before(){
2828
void loop() {
2929
float result;
3030
blink_before();
31-
//bool ok = client.call("mult", result, 2.0, 3.0);
32-
bool ok = client.call("divi", result, 2.0, 0.0);
31+
32+
bool ok = client.call("mult", result, 2.0, 3.0);
33+
34+
if (ok) {
35+
Serial.print("Result: ");
36+
Serial.println(result);
37+
}
38+
39+
ok = client.call("divi", result, 2.0, 0.0);
3340

3441
if (ok) {
3542
Serial.print("Result: ");

src/client.h

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,33 @@ class RPCClient {
4141
MsgPack::object::nil_t nil;
4242
RpcError rpc_error;
4343

44-
MsgPack::arr_size_t resp_size(4);
45-
46-
if (!unpacker.deserialize(resp_size, r_msg_type, r_msg_id, nil, result)){
47-
//Try to deserialize for a RpcError
48-
if (!unpacker.deserialize(resp_size, r_msg_type, r_msg_id, rpc_error, nil)){
49-
Serial.println("Unable to deserialize");
50-
Serial.print("buf len ");
51-
Serial.print(raw_buffer_fill);
52-
Serial.print(": ");
53-
for (size_t i = 0; i<raw_buffer_fill; i++){
54-
Serial.print(raw_buffer[i], HEX);
55-
Serial.print("-");
56-
}
57-
flush_buffer();
58-
return false;
59-
} else {
60-
Serial.print("RPC produced an error: ");
61-
Serial.println(rpc_error.code);
62-
Serial.println(rpc_error.traceback);
63-
}
44+
MsgPack::arr_size_t resp_size;
6445

46+
if (!unpacker.deserialize(resp_size, r_msg_type, r_msg_id)){
47+
Serial.println("malformed response");
48+
};
49+
50+
if ((resp_size.size() != 4) || (r_msg_type != 1) || (r_msg_id != msg_id)){
51+
Serial.println("wrong msg received");
52+
flush_buffer();
53+
return false;
6554
}
6655

67-
if (r_msg_id != msg_id){
68-
//Serial.println("msg_id mismatch");
56+
if (!unpacker.unpackNil()){
57+
Serial.print("RPC error - ");
58+
int error_code;
59+
MsgPack::str_t error_str;
60+
unpacker.deserialize(error_code, error_str);
61+
Serial.print(" error code: ");
62+
Serial.print(error_code);
63+
Serial.print(" error str: ");
64+
Serial.println(error_str);
65+
unpacker.unpackNil();
66+
msg_id += 1;
67+
flush_buffer();
68+
return false;
69+
} else if (!unpacker.deserialize(result)){
70+
Serial.println("Unexpected result");
6971
flush_buffer();
7072
return false;
7173
}

0 commit comments

Comments
 (0)