AdvancedClimateSystems / Tolk

Tolk exposes a JSON-RPC API to talk Modbus over RTU or TCP
Mozilla Public License 2.0
2 stars 0 forks source link

Implement custom JSON-RPC Errors. #2

Closed OrangeTux closed 8 years ago

OrangeTux commented 9 years ago

Currently when a Modbus server returns an Modbus error code, Tolk returns a JSON-RPC error with code -32603: Internal JSON-RPC error containing the stacktrace. Response below fails with Modbus exception code 2: Illegal Data Address.

{
    "error": {
        "code": -32603,
        "data": "Traceback (most recent call last):\n  File \"/home/auke/projects/tolk/.env/local/lib/python2.7/site-packages/pyjsonrpc/rpclib.py\", line 115, in call\n    result = rpc_function(*positional_params, **named_params)\n  File \"/home/auke/projects/tolk/scripts/../tolk/json_rpc.py\", line 25, in read_coils\n    int(quantity))\n  File \"/home/auke/projects/tolk/.env/local/lib/python2.7/site-packages/modbus_tk/utils.py\", line 26, in new\n    raise excpt\nModbusError: Modbus Error: Exception code = 2\n",
        "message": "Internal JSON-RPC error."
    },
    "id": 246344073102444981514353044232564665595,
    "jsonrpc": "2.0"
}

The JSON-RPC specification leaves room for custom errors. Below the surface Tolk uses Modbus-TK which implements 7 Modbus error codes. Tolk should return custom JSON-RPC errors reflecting the Modbus error code that was returned. Tolk should implement the following error codes:

An error response now could look like this:

{
    "error": {
        "code": -32001,
        "data": "Function code 27 is not  valid."
    },
    "id": 246344073102444981514353044232564665595,
    "jsonrpc": "2.0"
}
OrangeTux commented 8 years ago

For testing a Modbus server is created using modbus-tk. Under some circumstances the server stops working with this message:

2015-11-19 11:09:50,260 ERROR modbus_tcp._send Thread-14 Error while flushing the socket: flush_socket: maximum number of iterations reached

I've tried to get around it, but I couldn't. I think this is a bug in modbus-tk and not in Tolk. Because soon we'll replace modbus-tk and with uModbus I don't want to put more effort in it to fix it. Mark the test as with @pytest.mark.xfail to ignore the failure for now.

I guess test will pass again when we switched to uModbus.