Skip to content

Commit

Permalink
Fix verbose when unpack fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Mar 3, 2025
1 parent d19de78 commit 16d95a4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
8 changes: 6 additions & 2 deletions counterparty-core/counterpartycore/lib/api/apiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,9 @@ def handle_route(**kwargs):
) as e:
return return_result(400, error=str(e), start_time=start_time, query_args=query_args)
except Exception as e: # pylint: disable=broad-except
# import traceback
# print(traceback.format_exc())
import traceback

Check warning

Code scanning / pylint

Import outside toplevel (traceback). Warning

Import outside toplevel (traceback).

Check warning on line 364 in counterparty-core/counterpartycore/lib/api/apiserver.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/api/apiserver.py#L364

Added line #L364 was not covered by tests

print(traceback.format_exc())

Check warning on line 366 in counterparty-core/counterpartycore/lib/api/apiserver.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/api/apiserver.py#L366

Added line #L366 was not covered by tests
capture_exception(e)
logger.error("Error in API: %s", e)
return return_result(
Expand Down Expand Up @@ -411,6 +412,9 @@ def handle_route(**kwargs):
query_args=query_args,
)
except Exception as e: # pylint: disable=broad-except
import traceback

Check warning

Code scanning / pylint

Import outside toplevel (traceback). Warning

Import outside toplevel (traceback).

Check warning on line 415 in counterparty-core/counterpartycore/lib/api/apiserver.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/api/apiserver.py#L415

Added line #L415 was not covered by tests

print(traceback.format_exc())

Check warning on line 417 in counterparty-core/counterpartycore/lib/api/apiserver.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/api/apiserver.py#L417

Added line #L417 was not covered by tests
capture_exception(e)
logger.error("Error in API: %s", e)
return return_result(500, error="Internal server error")
Expand Down
46 changes: 40 additions & 6 deletions counterparty-core/counterpartycore/lib/api/verbose.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,17 @@ def inject_issuances_and_block_times(ledger_db, state_db, result_list):
item = result_item
if "params" in item:
item = item["params"]
if "unpacked_data" in item and item["unpacked_data"]:
if (
"unpacked_data" in item
and item["unpacked_data"]
and "message_data" in item["unpacked_data"]
):
item = item["unpacked_data"]["message_data"]
elif "unpacked_data" in item and item["unpacked_data"]:
elif (
"unpacked_data" in item
and item["unpacked_data"]
and "message_data" in item["unpacked_data"]
):
item = item["unpacked_data"]["message_data"]
for field_name in asset_fields:
if isinstance(item, list):
Expand Down Expand Up @@ -115,9 +123,17 @@ def inject_issuances_and_block_times(ledger_db, state_db, result_list):
item["params"][field_name_time] = block_times[item["params"][field_name]]
if "params" in item:
item = item["params"]
if "unpacked_data" in item and item["unpacked_data"]:
if (
"unpacked_data" in item
and item["unpacked_data"]
and "message_data" in item["unpacked_data"]
):
item = item["unpacked_data"]["message_data"]
elif "unpacked_data" in item and item["unpacked_data"]:
elif (
"unpacked_data" in item
and item["unpacked_data"]
and "message_data" in item["unpacked_data"]
):
item = item["unpacked_data"]["message_data"]
for field_name in asset_fields:
if isinstance(item, list):
Expand Down Expand Up @@ -233,7 +249,11 @@ def inject_normalized_quantities(result_list):
{"divisible": field_info["divisible"]},
)
)
if "unpacked_data" in item and item["unpacked_data"]:
if (
"unpacked_data" in item
and item["unpacked_data"]
and "message_data" in item["unpacked_data"]
):
item["unpacked_data"]["message_data"] = inject_normalized_quantity(
item["unpacked_data"]["message_data"],
field_name,
Expand All @@ -254,6 +274,7 @@ def inject_normalized_quantities(result_list):
if (
"unpacked_data" in item
and item["unpacked_data"]
and "message_data" in item["unpacked_data"]
and isinstance(item["unpacked_data"]["message_data"], list)
): # mpma send
for pos, sub_item in enumerate(item["unpacked_data"]["message_data"]):
Expand All @@ -267,6 +288,7 @@ def inject_normalized_quantities(result_list):
"params" in item
and "unpacked_data" in item["params"]
and item["params"]["unpacked_data"]
and "message_data" in item["unpacked_data"]
and isinstance(item["params"]["unpacked_data"]["message_data"], list)
): # mpma send
for pos, sub_item in enumerate(item["params"]["unpacked_data"]["message_data"]):
Expand All @@ -285,6 +307,7 @@ def inject_normalized_quantities(result_list):
"params" in item
and "unpacked_data" in item["params"]
and item["params"]["unpacked_data"]
and "message_data" in item["unpacked_data"]
and field_info["asset_field"] in item["params"]["unpacked_data"]["message_data"]
):
asset_info = item["params"]["unpacked_data"]["message_data"][
Expand All @@ -293,6 +316,7 @@ def inject_normalized_quantities(result_list):
elif (
"unpacked_data" in item
and item["unpacked_data"]
and "message_data" in item["unpacked_data"]
and field_info["asset_field"] in item["unpacked_data"]["message_data"]
):
asset_info = item["unpacked_data"]["message_data"][field_info["asset_field"]]
Expand All @@ -305,6 +329,7 @@ def inject_normalized_quantities(result_list):
elif (
"unpacked_data" in item
and item["unpacked_data"]
and "message_data" in item["unpacked_data"]
and "divisible" in item["unpacked_data"]["message_data"]
):
asset_info = {"divisible": item["unpacked_data"]["message_data"]["divisible"]}
Expand All @@ -318,12 +343,14 @@ def inject_normalized_quantities(result_list):
"params" in item
and "unpacked_data" in item["params"]
and item["params"]["unpacked_data"]
and "message_data" in item["unpacked_data"]
and "asset_info" in item["params"]["unpacked_data"]["message_data"]
):
asset_info = item["params"]["unpacked_data"]["message_data"]["asset_info"]
elif (
"unpacked_data" in item
and item["unpacked_data"]
and "message_data" in item["unpacked_data"]
and "asset_info" in item["unpacked_data"]["message_data"]
):
asset_info = item["unpacked_data"]["message_data"]["asset_info"]
Expand All @@ -340,6 +367,7 @@ def inject_normalized_quantities(result_list):
"params" in item
and "unpacked_data" in item["params"]
and item["params"]["unpacked_data"]
and "message_data" in item["unpacked_data"]
and field_name in item["params"]["unpacked_data"]["message_data"]
):
item["params"]["unpacked_data"]["message_data"] = inject_normalized_quantity( # noqa
Expand All @@ -348,6 +376,7 @@ def inject_normalized_quantities(result_list):
if (
"unpacked_data" in item
and item["unpacked_data"]
and "message_data" in item["unpacked_data"]
and field_name in item["unpacked_data"]["message_data"]
):
item["unpacked_data"]["message_data"] = inject_normalized_quantity( # noqa
Expand Down Expand Up @@ -440,7 +469,12 @@ def inject_unpacked_data_in_dict(ledger_db, item):
data = binascii.hexlify(item["data"]) if isinstance(item["data"], bytes) else item["data"]
if data:
block_index = item.get("block_index")
item["unpacked_data"] = compose.unpack(ledger_db, data, block_index=block_index)
try:
item["unpacked_data"] = compose.unpack(ledger_db, data, block_index=block_index)
except Exception: # pylint: disable=broad-exception-caught
item["unpacked_data"] = {

Check warning on line 475 in counterparty-core/counterpartycore/lib/api/verbose.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/api/verbose.py#L474-L475

Added lines #L474 - L475 were not covered by tests
"error": "Could not unpack data",
}
return item


Expand Down

0 comments on commit 16d95a4

Please sign in to comment.