Skip to content

Commit 48ba4b1

Browse files
hoodmanemaxvp
authored andcommitted
Fixes to Python debugging-logs example (#24479)
* `traceback.format_exc()` returns a string so we shouldn't json.dumps() it. * `traceback.format_exc()` should always return a truthy value so `or e` does nothing. * The coroutine returned by `post_log()` is not callable so it doens't make sense to call `create_once_callable()` on it. We want `create_proxy()`. * extract post_log function to top level * explicitly convert error to string for clarity when assigning to headers * `str(e)` already includes a traceback so we don't need to include a separate traceback here
1 parent b5b2989 commit 48ba4b1

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/content/docs/workers/examples/debugging-logs.mdx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,16 @@ export default {
116116

117117
```py
118118
from workers import WorkerEntrypoint
119-
import json
120-
import traceback
121-
from pyodide.ffi import create_once_callable
122-
from js import Response, fetch, Headers
119+
from pyodide.ffi import create_proxy
120+
from js import Response, fetch
121+
122+
async def post_log(data):
123+
log_url = "https://log-service.example.com/"
124+
await fetch(log_url, method="POST", body=data)
123125

124126
class Default(WorkerEntrypoint):
125127
async def fetch(self, request, _env, ctx):
126128
# Service configured to receive logs
127-
log_url = "https://log-service.example.com/"
128-
129-
async def post_log(data):
130-
return await fetch(log_url, method="POST", body=data)
131-
132129
response = await fetch(request)
133130

134131
try:
@@ -139,12 +136,10 @@ class Default(WorkerEntrypoint):
139136
except Exception as e:
140137
# Without ctx.waitUntil(), your fetch() to Cloudflare's
141138
# logging service may or may not complete
142-
ctx.waitUntil(create_once_callable(post_log(e)))
143-
stack = json.dumps(traceback.format_exc()) or e
139+
ctx.waitUntil(create_proxy(post_log(str(e))))
144140
# Copy the response and add to header
145141
response = Response.new(stack, response)
146-
response.headers["X-Debug-stack"] = stack
147-
response.headers["X-Debug-err"] = e
142+
response.headers["X-Debug-err"] = str(e)
148143

149144
return response
150145
```

0 commit comments

Comments
 (0)