Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/writer/app_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ def _handle_session_init(
if session is None:
raise MessageHandlingException("Session rejected.")

for mail in payload.additionalMail:
session.session_state.add_log_entry(**mail.model_dump())

user_state = {}
try:
user_state = session.session_state.user_state.to_dict()
Expand Down
16 changes: 15 additions & 1 deletion src/writer/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import tempfile
import textwrap
import time
import traceback
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

find . -name "serve.py" -path "*/writer/*" | head -20

Repository: writer/writer-framework

Length of output: 88


🏁 Script executed:

wc -l src/writer/serve.py

Repository: writer/writer-framework

Length of output: 91


🏁 Script executed:

sed -n '10,25p' src/writer/serve.py

Repository: writer/writer-framework

Length of output: 365


🏁 Script executed:

sed -n '960,980p' src/writer/serve.py

Repository: writer/writer-framework

Length of output: 625


🏁 Script executed:

head -20 src/writer/serve.py

Repository: writer/writer-framework

Length of output: 429


🏁 Script executed:

sed -n '967,976p' src/writer/serve.py

Repository: writer/writer-framework

Length of output: 430


🏁 Script executed:

grep -n "except Exception" src/writer/serve.py

Repository: writer/writer-framework

Length of output: 312


🏁 Script executed:

grep -n "logging\." src/writer/serve.py | head -20

Repository: writer/writer-framework

Length of output: 373


🏁 Script executed:

sed -n '590,595p' src/writer/serve.py

Repository: writer/writer-framework

Length of output: 328


🏁 Script executed:

sed -n '787,805p' src/writer/serve.py

Repository: writer/writer-framework

Length of output: 1038


🏁 Script executed:

sed -n '14,18p' src/writer/serve.py

Repository: writer/writer-framework

Length of output: 178


Add logging to the exception handler to prevent silent failures.

The bare except Exception at line 969 should include explicit logging. Currently, if no session is created, the error remains invisible. Add logging.exception() and a # noqa: BLE001 comment to address the broad exception catch while making failures visible.

🛠️ Suggested adjustment
     if enable_server_setup is True:
         try:
             _execute_server_setup_hook(user_app_path)
-        except Exception as e:
+        except Exception as e:  # noqa: BLE001
+            logging.exception("Custom server setup failed")
             custom_server_setup_mail.append(
                 {
                     "type": "error",
                     "title": "Custom server setup error",
                     "message": str(e),
                     "code": traceback.format_exc()
                 }
             )

Also applies to: 967-976

🤖 Prompt for AI Agents
In `@src/writer/serve.py` at line 16, The broad bare except in src/writer/serve.py
(around the block spanning lines 967–976) must log the exception so failures
aren't silent: in the except Exception handler, call logging.exception(...) to
record the stacktrace and error context, and append a "# noqa: BLE001" comment
to the except line to acknowledge the broad catch; update any local variable
references in the log message to provide context (e.g., session or request
identifiers) and ensure the logging module is imported at the top of the file if
not already.

import typing
from contextlib import asynccontextmanager, suppress
from importlib.machinery import ModuleSpec
Expand Down Expand Up @@ -121,6 +122,8 @@ def get_asgi_app(
global app
if serve_mode not in ["run", "edit"]:
raise ValueError("""Invalid mode. Must be either "run" or "edit".""")

custom_server_setup_mail: list[dict] = []

_fix_mimetype()
app_runner = AppRunner(user_app_path, serve_mode)
Expand Down Expand Up @@ -351,6 +354,7 @@ async def init(
cookies=dict(request.cookies),
headers=dict(request.headers),
proposedSessionId=initBody.proposedSessionId,
additionalMail=custom_server_setup_mail,
)
)

Expand Down Expand Up @@ -960,7 +964,17 @@ async def stream(websocket: WebSocket):

# Return
if enable_server_setup is True:
_execute_server_setup_hook(user_app_path)
try:
_execute_server_setup_hook(user_app_path)
except Exception as e:
custom_server_setup_mail.append(
{
"type": "error",
"title": "Custom server setup error",
"message": str(e),
"code": traceback.format_exc()
}
)

return app

Expand Down
8 changes: 8 additions & 0 deletions src/writer/ss_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,18 @@ class AppProcessServerRequest(BaseModel):
payload: Optional[Any] = None


class AdditionalMail(BaseModel):
type: Literal["info", "error"]
title: str
message: str
code: Optional[str] = None


class InitSessionRequestPayload(BaseModel):
cookies: Optional[Dict[str, str]] = None
headers: Optional[Dict[str, str]] = None
proposedSessionId: Optional[str] = None
additionalMail: list[AdditionalMail] = []


class InitSessionRequest(AppProcessServerRequest):
Expand Down