Skip to content

Conversation

@devin-ai-integration
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot commented Sep 25, 2025

Fixes #57

fix: enhance JSON parsing error messages with response content for DXP-687

Summary

Enhanced JSON parsing error messages in GenLayerProvider.make_request() to include raw response content when JSON parsing fails. This addresses DXP-687 where developers couldn't debug production issues because they only saw JSON parsing errors like "Expecting value: line 1 column 1 (char 0)" without knowing what the server actually returned.

Changes:

  • Capture response.text before JSON parsing attempt
  • Include response content in error message when JSON parsing fails
  • Truncate response content to 500 characters max with "..." ellipsis if longer
  • Preserve existing exception chaining behavior with from err
  • Maintain backward compatibility with existing error message format

Before: sim_getTransactionsForAddress returned invalid JSON: Expecting value: line 1 column 1 (char 0)
After: sim_getTransactionsForAddress returned invalid JSON: Expecting value: line 1 column 1 (char 0). Response content: <html>404 Not Found</html>

Review & Testing Checklist for Human

  • Test with actual malformed server responses - Point the SDK to an endpoint that returns HTML/malformed JSON and verify error messages are helpful and properly formatted
  • Verify truncation logic edge cases - Test with empty responses, very long responses (>500 chars), and responses containing special characters
  • Check for sensitive data exposure - Ensure the response content logging doesn't expose credentials, tokens, or other sensitive data in production logs
  • Validate exception chaining - Confirm that e.__cause__ still contains the original ValueError for proper error handling
  • Regression testing - Verify existing error handling patterns and successful JSON parsing still work as expected

Notes

  • This change affects all RPC methods processed through the provider (accounts, contracts, transactions, etc.)
  • The 500-character limit balances debugging utility with log safety/performance
  • Created comprehensive unit tests to verify truncation, empty responses, and exception chaining behavior

Requested by: daniel@genlayerlabs.com (@danielrc888)
Link to Devin run: https://app.devin.ai/sessions/1068b801ce04473787d47de8de44fcfd

- Include raw response content in JSON parsing errors for better debugging
- Truncate response content to 500 characters with ellipsis if longer
- Preserve exception chaining behavior with 'from err'
- Maintain backward compatibility with existing error message format
- Handle empty/None response text gracefully

Addresses DXP-687 where developers couldn't debug production issues
due to JSON parsing errors lacking actual response content.

Co-Authored-By: daniel@genlayerlabs.com <danielrojasc888@gmail.com>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@coderabbitai
Copy link

coderabbitai bot commented Sep 25, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

@danieljrc888
Copy link
Contributor

Using this script

from genlayer_py import create_client, create_account
from genlayer_py.chains import studionet
from genlayer_py.exceptions import GenLayerError

def test_studio_prod_json_error():
    """Test enhanced JSON error messages against studio prod"""
    print("Testing enhanced JSON error messages against studio prod...")
    print(f"Studio prod endpoint: {studionet.rpc_urls['default']['http'][0]}")
    
    account = create_account()
    client = create_client(chain=studionet, account=account)
    
    wallet_address = "0x1234567890123456789012345678901234567890"
    
    try:
        print(f"Calling sim_getTransactionsForAddress with wallet: {wallet_address}")
        
        response = client.provider.make_request(
            method="sim_getTransactionsForAddress",
            params=[wallet_address]
        )
        print("✓ Request succeeded - no JSON parsing error occurred")
        print(f"Response: {response}")
        print(f"Response type: {type(response)}")
        print(f"Response keys: {list(response.keys()) if isinstance(response, dict) else 'Not a dict'}")
        
    except GenLayerError as e:
        error_msg = str(e)
        print(f"✓ GenLayerError caught: {error_msg}")
        
        if "returned invalid JSON" in error_msg and "Response content:" in error_msg:
            print("✅ Enhanced JSON error message detected!")
            print("✅ Error message includes response content as expected")
            
            if "Response content:" in error_msg:
                content_part = error_msg.split("Response content:")[1].strip()
                print(f"✅ Response content preview: {content_part[:100]}...")
                
        elif "returned invalid JSON" in error_msg:
            print("❌ JSON parsing error detected but missing response content")
            print("❌ Enhancement may not be working correctly")
            
        else:
            print(f"ℹ️  Different type of GenLayerError: {error_msg}")
            
    except Exception as e:
        print(f"❌ Unexpected error: {type(e).__name__}: {e}")

if __name__ == "__main__":
    test_studio_prod_json_error()

With a valid address I get

Testing enhanced JSON error messages against studio prod...
Studio prod endpoint: https://studio.genlayer.com/api
Calling sim_getTransactionsForAddress with wallet: 0x1234567890123456789012345678901234567890
✓ Request succeeded - no JSON parsing error occurred
Response: {'id': 1758831486783, 'jsonrpc': '2.0', 'result': []}
Response type: <class 'dict'>
Response keys: ['id', 'jsonrpc', 'result']

With an invalid address I get

Testing enhanced JSON error messages against studio prod...
Studio prod endpoint: https://studio.genlayer.com/api
Calling sim_getTransactionsForAddress with wallet: 0x12345678901234567890123456789012345678909
✓ GenLayerError caught: sim_getTransactionsForAddress failed (code=-32000): Incorrect address format. Please provide a valid address: 0x12345678901234567890123456789012345678909
ℹ️  Different type of GenLayerError: sim_getTransactionsForAddress failed (code=-32000): Incorrect address format. Please provide a valid address: 0x12345678901234567890123456789012345678909

@danieljrc888 danieljrc888 self-assigned this Sep 25, 2025
@cristiam86 cristiam86 merged commit 3af48cf into main Sep 25, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add content of response on error

3 participants