websockets: fix status codes
HTTP 404 and JSON-RPC -32601 are equivalent, so translate where necessary. Any other JRPC code received outside of the HTTP error range should be assigned a 500. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
f6876c15e4
commit
d5a55047ab
|
@ -231,13 +231,15 @@ class JsonRPC:
|
||||||
return
|
return
|
||||||
result = obj.get("result")
|
result = obj.get("result")
|
||||||
if result is None:
|
if result is None:
|
||||||
|
name = conn.client_data["name"]
|
||||||
error = obj.get("error")
|
error = obj.get("error")
|
||||||
msg = f"Invalid RPC Response: {obj}"
|
msg = f"Invalid Response: {obj}"
|
||||||
code = 500
|
code = -32600
|
||||||
if isinstance(error, dict):
|
if isinstance(error, dict):
|
||||||
msg = error.get("message", msg)
|
msg = error.get("message", msg)
|
||||||
code = error.get("code", code)
|
code = error.get("code", code)
|
||||||
ret = ServerError(msg, code)
|
msg = f"{name} rpc error: {code} {msg}"
|
||||||
|
ret = ServerError(msg, 418)
|
||||||
else:
|
else:
|
||||||
ret = result
|
ret = result
|
||||||
conn.resolve_pending_response(response_id, ret)
|
conn.resolve_pending_response(response_id, ret)
|
||||||
|
@ -256,9 +258,12 @@ class JsonRPC:
|
||||||
result = await method(*args, **kwargs)
|
result = await method(*args, **kwargs)
|
||||||
except TypeError as e:
|
except TypeError as e:
|
||||||
return self.build_error(
|
return self.build_error(
|
||||||
-32603, f"Invalid params:\n{e}", req_id, True)
|
-32602, f"Invalid params:\n{e}", req_id, True)
|
||||||
except ServerError as e:
|
except ServerError as e:
|
||||||
return self.build_error(e.status_code, str(e), req_id, True)
|
code = e.status_code
|
||||||
|
if code == 404:
|
||||||
|
code = -32601
|
||||||
|
return self.build_error(code, str(e), req_id, True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return self.build_error(-31000, str(e), req_id, True)
|
return self.build_error(-31000, str(e), req_id, True)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue