Skip to content

Commit e74a608

Browse files
dawidclaude
andcommitted
Fix startup failures at info log level with robust error handling
The addon was failing to start at info log level due to strict error handling (set -e) that caused exits when utility scripts failed to load. Changes: - Add comprehensive error handling for all script sourcing - Use 2>/dev/null to suppress debug output that breaks info mode - Check for function existence before calling them - Convert fatal errors to warnings and continue startup - Add detailed dashboard startup validation and logging - Export LOG_LEVEL for use in child scripts This allows the addon to start successfully at both info and debug log levels, with graceful fallbacks when utilities fail to load. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 12a272e commit e74a608

File tree

1 file changed

+64
-15
lines changed

1 file changed

+64
-15
lines changed

run.sh

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ set -e
55
CONFIG_PATH=$(bashio::config 'ha_config_path' || echo "/config")
66
LOG_LEVEL=$(bashio::config 'log_level' || echo "info")
77

8+
# Export log level for scripts
9+
export LOG_LEVEL
10+
811
# Validate log level
912
case "${LOG_LEVEL}" in
1013
debug|info|warning|error) ;;
@@ -64,28 +67,49 @@ fi
6467

6568
# Load security utilities if available
6669
if [[ -f "/opt/scripts/security-utils.sh" ]]; then
67-
source "/opt/scripts/security-utils.sh"
68-
bashio::log.info "Security utilities loaded"
69-
70-
# Validate configuration path
71-
if ! validate_config_path "${CONFIG_PATH}"; then
72-
bashio::log.error "Invalid configuration path: ${CONFIG_PATH}"
73-
exit 1
70+
if source "/opt/scripts/security-utils.sh" 2>/dev/null; then
71+
bashio::log.info "Security utilities loaded"
72+
73+
# Validate configuration path if function exists
74+
if command -v validate_config_path >/dev/null 2>&1; then
75+
if ! validate_config_path "${CONFIG_PATH}" 2>/dev/null; then
76+
bashio::log.warning "Configuration path validation failed: ${CONFIG_PATH}"
77+
# Don't exit - continue anyway
78+
fi
79+
fi
80+
else
81+
bashio::log.warning "Failed to load security utilities - continuing without them"
7482
fi
7583
fi
7684

7785
# Initialize caching if available
7886
if [[ -f "/opt/scripts/cache-manager.sh" ]]; then
79-
source "/opt/scripts/cache-manager.sh"
80-
cache_init
81-
bashio::log.info "Cache system initialized"
87+
if source "/opt/scripts/cache-manager.sh" 2>/dev/null; then
88+
if command -v cache_init >/dev/null 2>&1; then
89+
if cache_init 2>/dev/null; then
90+
bashio::log.info "Cache system initialized"
91+
else
92+
bashio::log.warning "Cache initialization failed - continuing without cache"
93+
fi
94+
fi
95+
else
96+
bashio::log.warning "Failed to load cache utilities - continuing without them"
97+
fi
8298
fi
8399

84100
# Initialize circuit breakers if available
85101
if [[ -f "/opt/scripts/circuit-breaker.sh" ]]; then
86-
source "/opt/scripts/circuit-breaker.sh"
87-
cb_init "mcp_server"
88-
bashio::log.info "Circuit breaker system initialized"
102+
if source "/opt/scripts/circuit-breaker.sh" 2>/dev/null; then
103+
if command -v cb_init >/dev/null 2>&1; then
104+
if cb_init "mcp_server" 2>/dev/null; then
105+
bashio::log.info "Circuit breaker system initialized"
106+
else
107+
bashio::log.warning "Circuit breaker initialization failed - continuing without it"
108+
fi
109+
fi
110+
else
111+
bashio::log.warning "Failed to load circuit breaker utilities - continuing without them"
112+
fi
89113
fi
90114

91115
# Function to handle signals for graceful shutdown
@@ -116,11 +140,36 @@ fi
116140
# Start Dashboard API on port 3001 if files exist
117141
if [[ -f "/dashboard/api/server.js" ]]; then
118142
bashio::log.info "Starting Dashboard API on port 3001..."
119-
cd /dashboard/api
143+
cd /dashboard/api || {
144+
bashio::log.error "Failed to change to dashboard directory"
145+
exit 1
146+
}
147+
148+
# Test node and dependencies first
149+
if ! node --version >/dev/null 2>&1; then
150+
bashio::log.error "Node.js not available"
151+
exit 1
152+
fi
153+
154+
if ! ls node_modules >/dev/null 2>&1; then
155+
bashio::log.warning "No node_modules found, dashboard may not work"
156+
fi
157+
120158
nohup node server.js >/tmp/dashboard.log 2>&1 &
121159
DASHBOARD_PID=$!
122160
bashio::log.info "Dashboard API started with PID: $DASHBOARD_PID"
123-
cd /
161+
162+
# Give dashboard time to start
163+
sleep 2
164+
165+
# Check if still running
166+
if ! kill -0 $DASHBOARD_PID 2>/dev/null; then
167+
bashio::log.error "Dashboard API failed to start"
168+
bashio::log.error "Dashboard logs:"
169+
cat /tmp/dashboard.log 2>/dev/null || bashio::log.error "No dashboard logs"
170+
fi
171+
172+
cd / || true
124173
fi
125174

126175
# Run initial health check

0 commit comments

Comments
 (0)