diff --git a/server/database-storage.ts b/server/database-storage.ts index d3c113c..834e3cb 100644 --- a/server/database-storage.ts +++ b/server/database-storage.ts @@ -272,8 +272,13 @@ export class DatabaseStorage implements IStorage { // Webhook management async getWebhook(id: string): Promise { - const [webhook] = await db.select().from(webhooks).where(eq(webhooks.id, id)); - return webhook; + try { + const [webhook] = await db.select().from(webhooks).where(eq(webhooks.id, id)); + return webhook; + } catch (error) { + console.error("Error fetching webhook:", error); + return undefined; + } } async getWebhooksByOrganization(organizationId: string): Promise { @@ -372,15 +377,42 @@ export class DatabaseStorage implements IStorage { } async getWebhookDeliveryLog(id: string): Promise { - const [log] = await db.select().from(webhookDeliveryLogs).where(eq(webhookDeliveryLogs.id, id)); - return log; + try { + const [log] = await db.select().from(webhookDeliveryLogs).where(eq(webhookDeliveryLogs.id, id)); + return log; + } catch (error) { + console.error("Error fetching webhook delivery log:", error); + return undefined; + } } async getWebhookDeliveryLogsByWebhook(webhookId: string): Promise { - return await db.select() + try { + // Use a specific select list to avoid potential schema issues + // Only select columns that we know exist in the database + const logs = await db.select({ + id: webhookDeliveryLogs.id, + webhookId: webhookDeliveryLogs.webhookId, + status: webhookDeliveryLogs.status, + statusCode: webhookDeliveryLogs.statusCode, + message: webhookDeliveryLogs.message, + payload: webhookDeliveryLogs.payload, + responseBody: webhookDeliveryLogs.responseBody, + retryCount: webhookDeliveryLogs.retryCount, + createdAt: webhookDeliveryLogs.createdAt, + signature: webhookDeliveryLogs.signature, + // Use null for potentially missing fields + event: sql`NULL as event` + }) .from(webhookDeliveryLogs) .where(eq(webhookDeliveryLogs.webhookId, webhookId)) .orderBy(desc(webhookDeliveryLogs.createdAt)); + + return logs; + } catch (error) { + console.error("Error fetching webhook delivery logs:", error); + return []; // Return empty array on error + } } // DNS Metrics diff --git a/server/routes.ts b/server/routes.ts index cbd80a2..833938c 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -1018,6 +1018,7 @@ export async function registerRoutes(app: Express): Promise { // Get all delivery logs for a webhook app.get("/api/webhooks/:id/logs", requireRole(["admin", "manager"]), async (req, res) => { try { + // First check if the webhook exists const webhook = await storage.getWebhook(req.params.id); if (!webhook) { @@ -1029,10 +1030,17 @@ export async function registerRoutes(app: Express): Promise { return res.status(403).json({ message: "Not authorized to access logs for this webhook" }); } - const logs = await storage.getWebhookDeliveryLogsByWebhook(req.params.id); - res.json(logs); + try { + // Try to get logs with proper error handling + const logs = await storage.getWebhookDeliveryLogsByWebhook(req.params.id); + res.json(logs); + } catch (logError) { + // If there's a database schema issue, return an empty array instead of crashing + console.error("Error fetching webhook logs:", logError); + res.json([]); + } } catch (error) { - console.error("Error fetching webhook delivery logs:", error); + console.error("Error in webhook logs endpoint:", error); res.status(500).json({ message: "Internal server error" }); } });