From 50fd6aa10dbaf69b3f531eeb644b68a380955e61 Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Tue, 13 Feb 2024 13:41:42 +0100 Subject: [PATCH 1/4] Fix #16: JSONDecodeError while parsing error messages --- src/py_near/providers.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/py_near/providers.py b/src/py_near/providers.py index af6e749..a20ca2f 100644 --- a/src/py_near/providers.py +++ b/src/py_near/providers.py @@ -142,11 +142,15 @@ def get_error_from_response(content: dict): break if not body: return error - key, body = list(body.items())[0] - if key in ERROR_CODE_TO_EXCEPTION: - error = ERROR_CODE_TO_EXCEPTION[key]( - body, error_json=content["error"] - ) + if len(body) == 1 and list(body.keys())[0] in ERROR_CODE_TO_EXCEPTION: + key, body = list(body.items())[0] + if isinstance(body, str): + key = body + body = {} + if key in ERROR_CODE_TO_EXCEPTION: + error = ERROR_CODE_TO_EXCEPTION[key]( + body, error_json=content["error"] + ) else: break return error @@ -342,4 +346,4 @@ async def get_light_client_proof( return await self.json_rpc("light_client_proof", params) async def get_next_light_client_block(self, last_block_hash): - return await self.json_rpc("next_light_client_block", [last_block_hash]) + return await self.json_rpc("next_light_client_block", [last_block_hash]) \ No newline at end of file From 3593f7c4818d30d824a59857337c4d6fa85936b7 Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Tue, 13 Feb 2024 16:28:53 +0100 Subject: [PATCH 2/4] Make the condition better --- src/py_near/providers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/py_near/providers.py b/src/py_near/providers.py index a20ca2f..959c6b6 100644 --- a/src/py_near/providers.py +++ b/src/py_near/providers.py @@ -144,7 +144,7 @@ def get_error_from_response(content: dict): return error if len(body) == 1 and list(body.keys())[0] in ERROR_CODE_TO_EXCEPTION: key, body = list(body.items())[0] - if isinstance(body, str): + if body in ERROR_CODE_TO_EXCEPTION: key = body body = {} if key in ERROR_CODE_TO_EXCEPTION: From 8dd6c6055c51e7a39b3a70dc6f53d618ca40a211 Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Wed, 28 Feb 2024 13:46:21 +0100 Subject: [PATCH 3/4] Fix TypeError --- src/py_near/providers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/py_near/providers.py b/src/py_near/providers.py index 959c6b6..9888b4e 100644 --- a/src/py_near/providers.py +++ b/src/py_near/providers.py @@ -144,7 +144,7 @@ def get_error_from_response(content: dict): return error if len(body) == 1 and list(body.keys())[0] in ERROR_CODE_TO_EXCEPTION: key, body = list(body.items())[0] - if body in ERROR_CODE_TO_EXCEPTION: + if isinstance(body, str) and body in ERROR_CODE_TO_EXCEPTION: key = body body = {} if key in ERROR_CODE_TO_EXCEPTION: From de231b4f23b15150b226db4f996bfd1bd3d0cfcf Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Wed, 28 Feb 2024 13:54:06 +0100 Subject: [PATCH 4/4] Simplify --- src/py_near/providers.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/py_near/providers.py b/src/py_near/providers.py index 9888b4e..5a99559 100644 --- a/src/py_near/providers.py +++ b/src/py_near/providers.py @@ -147,10 +147,9 @@ def get_error_from_response(content: dict): if isinstance(body, str) and body in ERROR_CODE_TO_EXCEPTION: key = body body = {} - if key in ERROR_CODE_TO_EXCEPTION: - error = ERROR_CODE_TO_EXCEPTION[key]( - body, error_json=content["error"] - ) + error = ERROR_CODE_TO_EXCEPTION[key]( + body, error_json=content["error"] + ) else: break return error