使用cbpro调用Coinbase API的get_fills仅返回"message"无实际内容求助
I've run into similar quirky behavior with the cbpro library handling API errors unexpectedly, so let's break down how to get the actual error message instead of hitting that AttributeError.
The root problem here is that when the Coinbase API returns an error (like missing permissions, invalid product ID, or a delisted pair), the get_fills() generator doesn't properly parse the JSON error response—it ends up returning the string "message" instead of the full error object. Here's how to fix this and uncover the real error content:
Modified Code with Error Handling
import json all_fills = {} for product in products: all_fills[product] = [] fills = a.get_fills(product) try: for fill in fills: # Check if the response is a string (likely an error indicator) if isinstance(fill, str): print(f"⚠️ Unexpected string response for {product}: {fill}") # Try parsing it as JSON (matches Coinbase's documented error format) try: error_details = json.loads(fill) print(f"Full error message: {error_details.get('message', 'Unknown error')}") except json.JSONDecodeError: print("Could not parse the string into JSON—raw string shown above.") continue # Process valid fill objects normally for key, value in fill.items(): print(f"\t{key}: {value}") all_fills[product].append(fill) except Exception as e: print(f"Error processing {product}: {str(e)}") print(f"{product} captured.")
What This Does:
- Prevents the AttributeError: We first check if
fillis a string before calling.items()—this stops the crash dead in its tracks. - Uncovers Real Errors: Since Coinbase's API returns errors as JSON objects with a
"message"key, we try decoding the string to get context like "Product not found" or "Insufficient permissions". - Keeps Your Script Running: The outer
try-exceptblock ensures one problematic product doesn't take down the entire loop.
Why This Happens:
The cbpro library's pagination logic sometimes fails to handle non-200 API responses correctly. Instead of raising an explicit error, it yields fragments of the error response (like the string "message") as if it were a valid fill entry. This check helps you pinpoint the actual issue—whether it's a permission gap, a typo in the product ID, or a delisted pair.
内容的提问来源于stack exchange,提问作者dv151

