From 505aede8d38fee86ede25b6630e996e80d664940 Mon Sep 17 00:00:00 2001 From: ryan Date: Sat, 9 Aug 2025 18:54:42 -0700 Subject: [PATCH] Fix config file loading and MQTT connection error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Config file loading fixes: - Fix tilde expansion in config paths (~/.config/systant/systant.toml) - Add detailed debug logging for config file search paths - Properly expand home directory paths with System.user_home() MQTT connection improvements: - Add clearer connection status logging with broker host:port - Improve error handling in connection callback - Better error messages when connection fails or is lost - More detailed initial connection logging These fixes address production deployment issues where config files aren't found and MQTT connection errors aren't properly reported. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- server/lib/systant/config.ex | 23 ++++++++++++++++++++--- server/lib/systant/mqtt_client.ex | 1 + server/lib/systant/mqtt_handler.ex | 8 ++++++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/server/lib/systant/config.ex b/server/lib/systant/config.ex index eee5afd..a6260a3 100644 --- a/server/lib/systant/config.ex +++ b/server/lib/systant/config.ex @@ -171,9 +171,26 @@ defmodule Systant.Config do end defp find_config_file do - @default_config_paths - |> Enum.map(&Path.expand/1) - |> Enum.find(&File.exists?/1) + expanded_paths = @default_config_paths |> Enum.map(&expand_config_path/1) + + Logger.debug("Searching for config files at: #{inspect(expanded_paths)}") + + case Enum.find(expanded_paths, &File.exists?/1) do + nil -> + Logger.debug("No config file found, checked: #{inspect(expanded_paths)}") + nil + path -> + Logger.debug("Found config file at: #{path}") + path + end + end + + defp expand_config_path("~" <> rest) do + home = System.user_home() + Path.join(home, rest) + end + defp expand_config_path(path) do + Path.expand(path) end defp apply_env_overrides(config) do diff --git a/server/lib/systant/mqtt_client.ex b/server/lib/systant/mqtt_client.ex index 3b09b9b..debd717 100644 --- a/server/lib/systant/mqtt_client.ex +++ b/server/lib/systant/mqtt_client.ex @@ -16,6 +16,7 @@ defmodule Systant.MqttClient do mqtt_config = Systant.Config.mqtt_config(app_config) Logger.info("Starting MQTT client with config: #{inspect(mqtt_config)}") + Logger.info("Attempting to connect to MQTT broker at #{mqtt_config.host}:#{mqtt_config.port}") # Store both configs for later use state_config = %{ diff --git a/server/lib/systant/mqtt_handler.ex b/server/lib/systant/mqtt_handler.ex index d3ffd43..0fed72a 100644 --- a/server/lib/systant/mqtt_handler.ex +++ b/server/lib/systant/mqtt_handler.ex @@ -18,11 +18,15 @@ defmodule Systant.MqttHandler do def connection(status, state) do case status do :up -> - Logger.info("MQTT connection established") + Logger.info("MQTT connection established successfully") :down -> - Logger.warning("MQTT connection lost") + Logger.error("MQTT connection lost - check MQTT broker availability and configuration") :terminating -> Logger.info("MQTT connection terminating") + {:error, reason} -> + Logger.error("MQTT connection failed: #{inspect(reason)}") + other -> + Logger.error("MQTT connection status unknown: #{inspect(other)}") end {:ok, state} end