// src/lib/error-handler.ts
import { logger } from "@mesob/common";
import { HTTPException } from "hono/http-exception";
var isDatabaseError = (error) => {
  if (typeof error !== "object" || error === null) {
    return false;
  }
  if ("code" in error || "query" in error || "detail" in error) {
    return true;
  }
  if (error instanceof Error) {
    const message = error.message.toLowerCase();
    return message.includes("failed query") || message.includes("relation") || message.includes("column") || message.includes("syntax error") || message.includes("duplicate key") || message.includes("foreign key") || message.includes("null value");
  }
  return false;
};
var sanitizeDatabaseError = (error) => {
  const code = error.code;
  if (code === "23505") {
    return "Resource already exists";
  }
  if (code === "23503") {
    return "Referenced resource not found";
  }
  if (code === "23502") {
    return "Required field is missing";
  }
  if (code === "42P01") {
    return "Resource not found";
  }
  if (code === "42703") {
    return "Invalid request";
  }
  if (code === "23514") {
    return "Validation failed";
  }
  return "An error occurred while processing your request";
};
var isDatabaseErrorMessage = (message) => {
  const lowerMessage = message.toLowerCase();
  return lowerMessage.includes("failed query") || lowerMessage.includes("select") || lowerMessage.includes("insert") || lowerMessage.includes("update") || lowerMessage.includes("delete") || lowerMessage.includes("from") || lowerMessage.includes("where") || lowerMessage.includes("limit") || lowerMessage.includes("params:") || lowerMessage.includes("query") || message.includes('"iam".') || message.includes('"tenants"') || message.includes('"users"') || message.includes('"sessions"') || message.includes('"accounts"') || lowerMessage.includes("relation") || lowerMessage.includes("column") || lowerMessage.includes("syntax error") || lowerMessage.includes("database") || lowerMessage.includes("postgres") || lowerMessage.includes("sql");
};
var handleError = (error, c) => {
  logger.error("API Error:", {
    error,
    path: c.req.path,
    method: c.req.method,
    url: c.req.url
  });
  if (error instanceof HTTPException) {
    const message = isDatabaseErrorMessage(error.message) ? "An error occurred while processing your request" : error.message;
    return c.json({ error: message }, error.status);
  }
  if (isDatabaseError(error)) {
    const userMessage = sanitizeDatabaseError(error);
    logger.error("Database error details:", {
      code: error.code,
      message: error.message,
      detail: error.detail,
      query: error.query,
      parameters: error.parameters
    });
    return c.json({ error: userMessage }, 500);
  }
  if (error instanceof Error) {
    const message = error.message;
    const lowerMessage = message.toLowerCase();
    const isDatabaseError2 = lowerMessage.includes("failed query") || lowerMessage.includes("select") || lowerMessage.includes("insert") || lowerMessage.includes("update") || lowerMessage.includes("delete") || lowerMessage.includes("from") || lowerMessage.includes("where") || lowerMessage.includes("limit") || lowerMessage.includes("params:") || lowerMessage.includes("query") || message.includes('"iam".') || message.includes('"tenants"') || message.includes('"users"') || message.includes('"sessions"') || message.includes('"accounts"') || lowerMessage.includes("relation") || lowerMessage.includes("column") || lowerMessage.includes("syntax error") || lowerMessage.includes("duplicate key") || lowerMessage.includes("foreign key") || lowerMessage.includes("null value") || lowerMessage.includes("database") || lowerMessage.includes("postgres") || lowerMessage.includes("sql");
    if (isDatabaseError2) {
      logger.error("SQL/database error detected:", {
        message: error.message,
        stack: error.stack,
        name: error.name
      });
      return c.json(
        { error: "An error occurred while processing your request" },
        500
      );
    }
    logger.error("Error details:", {
      message: error.message,
      stack: error.stack,
      name: error.name
    });
    return c.json(
      { error: "An error occurred while processing your request" },
      500
    );
  }
  logger.error("Unknown error:", error);
  return c.json(
    { error: "An error occurred while processing your request" },
    500
  );
};
export {
  handleError
};
//# sourceMappingURL=error-handler.js.map