-
Notifications
You must be signed in to change notification settings - Fork 0
fix: add payload to exception to improve debugging capabilities #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,24 @@ | ||
| import json | ||
| import importlib.resources | ||
|
|
||
| with importlib.resources.as_file( | ||
| importlib.resources.files("genlayer_py.consensus.abi").joinpath( | ||
| "consensus_data_abi.json" | ||
| ) | ||
| ) as path, open(path, "r", encoding="utf-8") as f: | ||
| with ( | ||
| importlib.resources.as_file( | ||
| importlib.resources.files("genlayer_py.consensus.abi").joinpath( | ||
| "consensus_data_abi.json" | ||
| ) | ||
| ) as path, | ||
| open(path, "r", encoding="utf-8") as f, | ||
| ): | ||
| CONSENSUS_DATA_ABI = json.load(f) | ||
|
|
||
| with importlib.resources.as_file( | ||
| importlib.resources.files("genlayer_py.consensus.abi").joinpath( | ||
| "consensus_main_abi.json" | ||
| ) | ||
| ) as path, open(path, "r", encoding="utf-8") as f: | ||
| with ( | ||
| importlib.resources.as_file( | ||
| importlib.resources.files("genlayer_py.consensus.abi").joinpath( | ||
| "consensus_main_abi.json" | ||
| ) | ||
| ) as path, | ||
| open(path, "r", encoding="utf-8") as f, | ||
| ): | ||
| CONSENSUS_MAIN_ABI = json.load(f) | ||
|
|
||
| __all__ = ["CONSENSUS_DATA_ABI", "CONSENSUS_MAIN_ABI"] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,27 @@ | ||
| from typing import Any, Optional | ||
|
|
||
|
|
||
| class GenLayerError(Exception): | ||
| """Base exception class for GenLayer SDK errors. | ||
|
|
||
| This exception can carry additional context information through an optional | ||
| payload dictionary, useful for debugging and error handling. | ||
|
|
||
| Args: | ||
| message: Human-readable error description. | ||
| payload: Optional dictionary containing additional error context, | ||
| such as transaction details, error codes, or debug information. | ||
|
|
||
| Attributes: | ||
| payload: Dictionary of additional error context (None if not provided). | ||
|
|
||
| Example: | ||
| >>> raise GenLayerError( | ||
| ... "Transaction failed", | ||
| ... {"tx_hash": "0x123...", "reason": "insufficient funds"} | ||
| ... ) | ||
| """ | ||
| An error raised by GenLayer. | ||
| """ | ||
|
|
||
| def __init__(self, message: str, payload: Optional[dict[str, Any]] = None): | ||
| super().__init__(message) | ||
| self.payload = payload |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -38,9 +38,14 @@ def make_request( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| resp = response.json() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| except ValueError as err: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| response_preview = response.text[:500] if len(response.text) <= 500 else f"{response.text[:500]}..." | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| response_preview = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| response.text[:500] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(response.text) <= 500 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| else f"{response.text[:500]}..." | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise GenLayerError( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| f"{method} returned invalid JSON: {err}. Response content: {response_preview}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| f"{method} returned invalid JSON: {err}. Response content: {response_preview}", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| payload={"response": response}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) from err | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+41
to
49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid storing the raw Response object in payload Keeping the except ValueError as err:
response_preview = (
response.text[:500]
if len(response.text) <= 500
else f"{response.text[:500]}..."
)
+ response_payload = {
+ "status_code": response.status_code,
+ "headers": dict(response.headers),
+ "body_preview": response_preview,
+ }
raise GenLayerError(
f"{method} returned invalid JSON: {err}. Response content: {response_preview}",
- payload={"response": response},
+ payload=response_payload,
) from err📝 Committable suggestion
Suggested change
🧰 Tools🪛 Ruff (0.13.1)46-49: Avoid specifying long messages outside the exception class (TRY003) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
| self._raise_on_error(resp, method) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return resp | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid Python 3.10-only
with (...)syntax unless you’ve dropped 3.9 support.Using parentheses around multiple context managers (Line 4) is only valid starting in Python 3.10. If the package still supports Python 3.9 (or earlier), this will raise a
SyntaxErrorat import time and break every consumer. Please stick to the nestedwithform or otherwise confirm thatrequires-pythonhas been bumped to>=3.10across the project before landing this.🤖 Prompt for AI Agents