better logging
This commit is contained in:
@@ -75,27 +75,100 @@ def proxy():
|
|||||||
|
|
||||||
# Send the original 'incoming' data to the target
|
# Send the original 'incoming' data to the target
|
||||||
response = requests.post(TARGET_URL_HTTP, json=incoming)
|
response = requests.post(TARGET_URL_HTTP, json=incoming)
|
||||||
outgoing = response.json()
|
|
||||||
|
# Extract method name for error tracking
|
||||||
|
method_name = None
|
||||||
|
if isinstance(incoming, dict) and 'method' in incoming:
|
||||||
|
method_name = incoming['method']
|
||||||
|
|
||||||
|
# Handle HTTP errors
|
||||||
|
if response.status_code != 200:
|
||||||
|
# Log HTTP error
|
||||||
|
log_lines = [request_log]
|
||||||
|
error_log = f"<== HTTP Error {response.status_code}: {response.reason}"
|
||||||
|
log_lines.append(error_log)
|
||||||
|
|
||||||
|
# Try to include response body if it's not too large
|
||||||
|
try:
|
||||||
|
response_text = response.text[:1000] # Limit to first 1000 chars
|
||||||
|
if response_text:
|
||||||
|
log_lines.append(f"<== Response Body: {response_text}")
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
print('\n---\n'.join(log_lines), file=sys.stdout, flush=True)
|
||||||
|
|
||||||
|
# Track this as an error
|
||||||
|
if method_name:
|
||||||
|
error_methods.add(method_name)
|
||||||
|
|
||||||
|
# Create JSON-RPC error response
|
||||||
|
outgoing = {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"error": {
|
||||||
|
"code": -32603, # Internal error per JSON-RPC spec
|
||||||
|
"message": f"HTTP {response.status_code} from upstream server",
|
||||||
|
"data": {
|
||||||
|
"http_status": response.status_code,
|
||||||
|
"http_reason": response.reason
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if isinstance(incoming, dict) and 'id' in incoming:
|
||||||
|
outgoing['id'] = incoming['id']
|
||||||
|
else:
|
||||||
|
outgoing['id'] = None
|
||||||
|
|
||||||
|
return jsonify(outgoing)
|
||||||
|
|
||||||
|
# Try to parse JSON response
|
||||||
|
try:
|
||||||
|
outgoing = response.json()
|
||||||
|
except ValueError as e:
|
||||||
|
# Log JSON parsing error
|
||||||
|
log_lines = [request_log]
|
||||||
|
error_log = f"<== JSON Parse Error: {str(e)}"
|
||||||
|
log_lines.append(error_log)
|
||||||
|
log_lines.append(f"<== Response Body: {response.text[:500]}")
|
||||||
|
|
||||||
|
print('\n---\n'.join(log_lines), file=sys.stdout, flush=True)
|
||||||
|
|
||||||
|
# Track this as an error
|
||||||
|
if method_name:
|
||||||
|
error_methods.add(method_name)
|
||||||
|
|
||||||
|
# Create JSON-RPC error response
|
||||||
|
outgoing = {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"error": {
|
||||||
|
"code": -32603, # Internal error
|
||||||
|
"message": "Invalid JSON response from upstream server",
|
||||||
|
"data": str(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if isinstance(incoming, dict) and 'id' in incoming:
|
||||||
|
outgoing['id'] = incoming['id']
|
||||||
|
else:
|
||||||
|
outgoing['id'] = None
|
||||||
|
|
||||||
|
return jsonify(outgoing)
|
||||||
|
|
||||||
# Initialize log_lines here, decide what to include based on error status and ignored methods
|
# Initialize log_lines here, decide what to include based on error status and ignored methods
|
||||||
log_lines = []
|
log_lines = []
|
||||||
log_this_error = True # Flag to control logging of the current error
|
log_this_error = True # Flag to control logging of the current error
|
||||||
|
|
||||||
if 'error' in outgoing:
|
if 'error' in outgoing:
|
||||||
method_name = None
|
|
||||||
# First, check if the method itself dictates ignoring the error
|
# First, check if the method itself dictates ignoring the error
|
||||||
if isinstance(incoming, dict) and 'method' in incoming:
|
if method_name and method_name in ignored_error_methods:
|
||||||
method_name = incoming['method']
|
log_this_error = False
|
||||||
if method_name in ignored_error_methods:
|
print(f"INFO: Ignored error for method '{method_name}' (logging suppressed).", file=sys.stdout, flush=True)
|
||||||
log_this_error = False
|
|
||||||
print(f"INFO: Ignored error for method '{method_name}' (logging suppressed).", file=sys.stdout, flush=True)
|
|
||||||
|
|
||||||
# Second, if we haven't decided to ignore it yet, check the error code
|
# Second, if we haven't decided to ignore it yet, check the error code
|
||||||
#if log_this_error and isinstance(outgoing['error'], dict) and outgoing['error'].get('code') == 3:
|
if log_this_error and isinstance(outgoing['error'], dict) and outgoing['error'].get('code') == 3:
|
||||||
# log_this_error = False
|
log_this_error = False
|
||||||
# Optional: Log that an error was ignored due to its code
|
# Optional: Log that an error was ignored due to its code
|
||||||
# method_str = f" for method '{method_name}'" if method_name else ""
|
method_str = f" for method '{method_name}'" if method_name else ""
|
||||||
# print(f"INFO: Ignored error{method_str} due to error code 3 (logging suppressed).", file=sys.stdout, flush=True)
|
print(f"INFO: Ignored error{method_str} due to error code 3 (logging suppressed).", file=sys.stdout, flush=True)
|
||||||
|
|
||||||
# Only log the request/error response if log_this_error is True
|
# Only log the request/error response if log_this_error is True
|
||||||
if log_this_error:
|
if log_this_error:
|
||||||
@@ -110,9 +183,8 @@ def proxy():
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
# For success, log only the success message with the method name
|
# For success, log only the success message with the method name
|
||||||
method_name = "unknown_method" # Default if not found
|
if not method_name:
|
||||||
if isinstance(incoming, dict) and 'method' in incoming:
|
method_name = "unknown_method" # Default if not found
|
||||||
method_name = incoming['method']
|
|
||||||
response_log = f"<== Response (Success): Method '{method_name}' completed."
|
response_log = f"<== Response (Success): Method '{method_name}' completed."
|
||||||
log_lines.append(response_log)
|
log_lines.append(response_log)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user