Skip to content

Commit

Permalink
Truncate token uri in logging
Browse files Browse the repository at this point in the history
Some URLs are very long (e.g. data urls), so we truncate them to make them easier to read in logs and use less space.
  • Loading branch information
ligustah committed Mar 17, 2024
1 parent d497fa3 commit 46aabc4
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions offchain/metadata/pipelines/metadata_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
)


def _truncate_uri(uri: str, max_length: int = 100) -> str:
if len(uri) <= max_length:
return uri

keep_length = (max_length - 3) // 2 # 3 is for the '...'

return uri[:keep_length] + "..." + uri[-keep_length:]


class MetadataPipeline(BasePipeline):
"""Pipeline for processing NFT metadata.
Expand Down Expand Up @@ -161,7 +170,7 @@ def fetch_token_metadata(
try:
raw_data = self.fetcher.fetch_content(token.uri)
except Exception as e:
error_message = f"({token.chain_identifier}-{token.collection_address}-{token.token_id}) Failed to parse token uri: {token.uri}. {str(e)}" # noqa: E501
error_message = f"({token.chain_identifier}-{token.collection_address}-{token.token_id}) Failed to parse token uri: {_truncate_uri(token.uri)}. {str(e)}" # noqa: E501
logger.error(error_message)
possible_metadatas_or_errors.append(
MetadataProcessingError.from_token_and_error(
Expand Down Expand Up @@ -216,9 +225,9 @@ async def gen_fetch_token_metadata(
Union[Metadata, MetadataProcessingError]: returns either a Metadata
or a MetadataProcessingError if unable to parse.
"""
possible_metadatas_or_errors: list[
Union[Metadata, MetadataProcessingError]
] = []
possible_metadatas_or_errors: list[Union[Metadata, MetadataProcessingError]] = (
[]
)

if not token.uri:
return MetadataProcessingError.from_token_and_error(
Expand All @@ -230,7 +239,7 @@ async def gen_fetch_token_metadata(
try:
raw_data = await self.fetcher.gen_fetch_content(token.uri)
except Exception as e:
error_message = f"({token.chain_identifier}-{token.collection_address}-{token.token_id}) Failed to parse token uri: {token.uri}. {str(e)}" # noqa: E501
error_message = f"({token.chain_identifier}-{token.collection_address}-{token.token_id}) Failed to parse token uri: {_truncate_uri(token.uri)}. {str(e)}" # noqa: E501
logger.error(error_message)
possible_metadatas_or_errors.append(
MetadataProcessingError.from_token_and_error(
Expand Down

0 comments on commit 46aabc4

Please sign in to comment.