Skip to content

Commit

Permalink
Improve JSONRPC errors messages, use defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Dec 23, 2018
1 parent 14f47f8 commit a3692dc
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/aria2p/client.py
Expand Up @@ -2,11 +2,30 @@
import requests


DEFAULT_MSG_ID = -1
DEFAULT_ID = -1
DEFAULT_HOST = "http://localhost"
DEFAULT_PORT = 6800

JSONRPC_PARSER_ERROR = -32700
JSONRPC_INVALID_REQUEST = -32600
JSONRPC_METHOD_NOT_FOUND = -32601
JSONRPC_INVALID_PARAMS = -32602
JSONRPC_INTERNAL_ERROR = -32603

JSONRPC_CODES = {
JSONRPC_PARSER_ERROR: "Invalid JSON was received by the server.",
JSONRPC_INVALID_REQUEST: "The JSON sent is not a valid Request object.",
JSONRPC_METHOD_NOT_FOUND: "The method does not exist / is not available.",
JSONRPC_INVALID_PARAMS: "Invalid method parameter(s).",
JSONRPC_INTERNAL_ERROR: "Internal JSON-RPC error.",
}


class JSONRPCError(Exception):
def __init__(self, code, message):
if code in JSONRPC_CODES:
message = f"{JSONRPC_CODES[code]}\n{message}"

self.code = code
self.message = message

Expand Down Expand Up @@ -154,7 +173,7 @@ def get_payload(method, params=None, msg_id=None, as_json=True):
if msg_id is not None:
payload["id"] = msg_id
else:
payload["id"] = DEFAULT_MSG_ID
payload["id"] = DEFAULT_ID

if params:
payload["params"] = params
Expand Down

0 comments on commit a3692dc

Please sign in to comment.