From e7cd90b9058582a0c6abcc6f800a86b8774bd1a4 Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Fri, 5 Dec 2025 19:56:06 +0100 Subject: [PATCH] Fix error handling scope issue in service creation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The error handler was referencing variables (username, serviceFqn, offers) that were declared inside the try block. If an error occurred before these were defined, the error handler itself would fail, resulting in non-JSON responses that caused "JSON.parse: unexpected character" errors on the client. Fixed by: - Declaring variables at function scope - Initializing offers as empty array - Using destructuring assignment for username/serviceFqn This ensures the error handler can always access these variables safely, even if an early error occurs, and will always return proper JSON responses. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/app.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/app.ts b/src/app.ts index 28ff4f5..7f0541a 100644 --- a/src/app.ts +++ b/src/app.ts @@ -182,9 +182,14 @@ export function createApp(storage: Storage, config: Config) { * Publish a service */ app.post('/services', authMiddleware, async (c) => { + let username: string | undefined; + let serviceFqn: string | undefined; + let offers: any[] = []; + try { const body = await c.req.json(); - const { username, serviceFqn, sdp, ttl, isPublic, metadata, signature, message } = body; + ({ username, serviceFqn } = body); + const { sdp, ttl, isPublic, metadata, signature, message } = body; if (!username || !serviceFqn || !sdp) { return c.json({ error: 'Missing required parameters: username, serviceFqn, sdp' }, 400); @@ -230,7 +235,7 @@ export function createApp(storage: Storage, config: Config) { const expiresAt = Date.now() + offerTtl; // Create offer first - const offers = await storage.createOffers([{ + offers = await storage.createOffers([{ peerId, sdp, expiresAt