Skip to content

📋 Add error handling and logging for webhook debugging #10

Description

@IvyGain

Description

The webhook handler needs better error handling and logging to debug issues in production.

Current Issues

  1. No request logging - Cannot see what Lark is actually sending
  2. Silent failures - Errors may be swallowed without logging
  3. No response logging - Cannot verify what's being returned to Lark

Recommended Improvements

1. Add detailed request logging

@app.post("/webhook/event")
async def webhook_event(request: Request):
    # Log full request for debugging
    body_bytes = await request.body()
    headers = dict(request.headers)
    
    logger.info(f"=== WEBHOOK REQUEST ===")
    logger.info(f"Headers: {json.dumps(headers, default=str)}")
    logger.info(f"Body: {body_bytes.decode()[:1000]}")
    
    try:
        body = json.loads(body_bytes)
    except json.JSONDecodeError as e:
        logger.error(f"Failed to parse JSON: {e}")
        logger.error(f"Raw body: {body_bytes.decode()}")
        raise HTTPException(status_code=400, detail="Invalid JSON")

2. Add response logging

    response_data = {"status": "processed"}
    logger.info(f"=== WEBHOOK RESPONSE ===")
    logger.info(f"Response: {json.dumps(response_data)}")
    return JSONResponse(content=response_data)

3. Add structured error handling

except Exception as e:
    logger.exception(f"Webhook processing error: {e}")
    # Still return 200 to Lark to prevent retries
    return JSONResponse(
        content={"status": "error", "error": str(e)},
        status_code=200
    )

Also Add

  • Health check endpoint that shows recent webhook activity
  • Endpoint to view recent logs (for debugging)
  • Sentry or similar error tracking integration

Priority

MEDIUM - Essential for debugging production issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions