Skip to content

Commit

Permalink
Fix errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
BenediktBurger committed Feb 23, 2024
1 parent 1762eca commit d8875d5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: pyleco
channels:
- conda-forge
dependencies:
- pyzmq=25.1.2
- pyzmq #=25.1.2
- pip # don't pin, to gain newest conda compatibility fixes
- pip:
- openrpc==8.1.0
# - openrpc==8.1.0
- uuid7==0.1.0
# Development dependencies below
- pytest=7.2.0
Expand Down
2 changes: 2 additions & 0 deletions pyleco/json_utils/rpc_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def build_request_str(self, method: str, *args, **kwargs) -> str:
"and give additional keyword arguments at the same time.")
id = self._id_counter
self._id_counter += 1
r: Union[Request, ParamsRequest]
if args or kwargs:
r = ParamsRequest(id=id, method=method, params=kwargs or list(args))
else:
Expand All @@ -64,6 +65,7 @@ def get_result_from_response(self, data: Union[bytes, str, Dict]) -> Any:

# Raise error if JSON RPC error response.
if error_content := json_data.get("error"):
error: Union[Error, DataError]
if error_content.get("data"):
error = DataError(**error_content)
else:
Expand Down
15 changes: 6 additions & 9 deletions pyleco/json_utils/rpc_server_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,15 @@ def process_request(self, data: Union[bytes, str]) -> Optional[str]:
result = self.process_single_request(element)
if result is not None:
results.append(result.model_dump())
if results:
return json.dumps(results, separators=(',', ':'))
elif isinstance(json_data, dict):
results = self.process_single_request(json_data).model_dump()
if results:
return json.dumps(results, separators=(',', ':'))
results = self.process_single_request(json_data)
if results:
return results.model_dump_json()
except Exception as exc:
log.exception(f"{type(exc).__name__}:", exc_info=exc)
return ErrorResponse(id=id, error=INTERNAL_ERROR).model_dump_json()
else:
if id is not None:
return ResultResponse(id=id, result=result).model_dump_json()
else:
return None
return ErrorResponse(id=None, error=INTERNAL_ERROR).model_dump_json()

def process_single_request(self, request: Dict[str, Any]
) -> Union[ResultResponse, ErrorResponse, None]:
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ requires-python = ">=3.7"
dependencies = [
"pyzmq >= 22.3.0",
"openrpc >= 8.1.0;python_version>=3.9",
"jsonrpc2-objects >= 3.0.0;python_version>=3.9",
"jsonrpc2-objects >= 3.0.0; python_version >= '3.9'",
"uuid7 >= 0.1.0",
]

Expand Down Expand Up @@ -80,4 +80,6 @@ omit = [
# Omit LECO definitions
"pyleco/errors.py",
"pyleco/core/leco_protocols.py",
"pyleco/json_utils/json_objects",
"pyleco/json_utils/rpc_server",
]
2 changes: 1 addition & 1 deletion tests/json_utils/test_json_objects_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
def test_error_with_data():
data_error = jod.DataError(code=5, message="whatever", data="abc")
error_response = jod.ErrorResponse(id=7, error=data_error)
assert error_response.model_dump_json() == '{"id":7,"error":{"code":5,"message":"whatever","data":"abc"},"jsonrpc":"2.0"}'
assert error_response.model_dump_json() == '{"id":7,"error":{"code":5,"message":"whatever","data":"abc"},"jsonrpc":"2.0"}' # noqa

0 comments on commit d8875d5

Please sign in to comment.