Fix error handling scope issue in service creation

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 <noreply@anthropic.com>
This commit is contained in:
2025-12-05 19:56:06 +01:00
parent 67b1decbad
commit e7cd90b905

View File

@@ -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