Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions PasarGuardNodeBridge/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import logging
import math
import ssl
import time
from datetime import datetime, timezone
from enum import IntEnum
from json import JSONDecodeError
from typing import Optional
from typing import Any, Optional
from uuid import UUID

import aiohttp
Expand All @@ -25,6 +27,22 @@
DEFAULT_INTERNAL_TIMEOUT = 15 # Default timeout for internal gRPC/HTTP operations


class UTCFormatter(logging.Formatter):
converter = time.gmtime


def _json_safe(value: Any) -> Any:
if isinstance(value, datetime):
if value.tzinfo is None or value.utcoffset() is None:
raise NodeAPIError(code=-6, detail="Naive datetime values are not supported in JSON payloads")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Error code conflict: code=-6 is already used.

The error code -6 is already defined at line 119 for "Invalid proxy format". Using the same code for "Naive datetime values are not supported" creates ambiguity when handling errors.

🐛 Proposed fix: use a unique error code
-            raise NodeAPIError(code=-6, detail="Naive datetime values are not supported in JSON payloads")
+            raise NodeAPIError(code=-7, detail="Naive datetime values are not supported in JSON payloads")
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@PasarGuardNodeBridge/controller.py` at line 37, The raise of NodeAPIError for
naive datetime uses a duplicate code (-6) already used for "Invalid proxy
format"; locate the raise in controller.py (the NodeAPIError call with detail
"Naive datetime values are not supported in JSON payloads") and change its code
to a unique, unused error code (e.g., -7), and update any centralized error
registry, mappings, or tests that reference this specific error so the new code
is recognized; ensure the NodeAPIError instantiation and any downstream
handlers/logging reflect the new code.

return value.astimezone(timezone.utc).isoformat().replace("+00:00", "Z")
if isinstance(value, dict):
return {key: _json_safe(val) for key, val in value.items()}
if isinstance(value, (list, tuple)):
return [_json_safe(item) for item in value]
return value


class NodeAPIError(Exception):
def __init__(self, code, detail):
self.code = code
Expand Down Expand Up @@ -61,7 +79,12 @@ def __init__(
logger = logging.getLogger(self.name)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
handler.setFormatter(
UTCFormatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%dT%H:%M:%SZ",
)
)
logger.addHandler(handler)
self.logger = logger

Expand Down Expand Up @@ -532,7 +555,7 @@ async def _make_json_request(

try:
async with self._json_client.request(
method=method, url=endpoint, json=json, timeout=make_timeout(timeout)
method=method, url=endpoint, json=_json_safe(json), timeout=make_timeout(timeout)
) as raw_response:
response = await buffer_response(raw_response)
response.raise_for_status()
Expand Down