added logs for all errors

This commit is contained in:
ranjith
2025-07-28 06:18:26 +00:00
parent b1609bb4d4
commit 75e401c52f

View File

@@ -169,7 +169,7 @@ func messageHandler(client mqtt.Client, msg mqtt.Message) {
rows, err := db.Query(paramQuery, mfmMeterID, plantID)
if err != nil {
publishError(client, errorTopic, fmt.Sprintf("❌ Failed to fetch parameters for meter %d: %v", mfmMeterID, err))
logAndPublishError(client, errorTopic, fmt.Sprintf("❌ Failed to fetch parameters for meter %d: %v", mfmMeterID, err))
return
}
defer rows.Close()
@@ -178,25 +178,25 @@ func messageHandler(client mqtt.Client, msg mqtt.Message) {
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
publishError(client, errorTopic, fmt.Sprintf("❌ Failed to scan parameter name: %v", err))
logAndPublishError(client, errorTopic, fmt.Sprintf("❌ Failed to scan parameter name: %v", err))
return
}
paramColumns = append(paramColumns, name)
}
if len(paramColumns) == 0 {
publishError(client, errorTopic, fmt.Sprintf("❌ No parameters found for meter_id %d", mfmMeterID))
logAndPublishError(client, errorTopic, fmt.Sprintf("❌ No parameters found for meter_id %d", mfmMeterID))
return
}
// Validate values count
if len(values) < len(paramColumns) {
publishError(client, errorTopic, fmt.Sprintf("❌ Insufficient register values: expected %d, got %d", len(paramColumns), len(values)))
logAndPublishError(client, errorTopic, fmt.Sprintf("❌ Insufficient register values: expected %d, got %d", len(paramColumns), len(values)))
return
}
if len(values) > len(paramColumns){
publishError(client, errorTopic, fmt.Sprintf("❌ Too many register values: expected %d, got %d", len(paramColumns), len(values)))
logAndPublishError(client, errorTopic, fmt.Sprintf("❌ Too many register values: expected %d, got %d", len(paramColumns), len(values)))
return
}
@@ -231,7 +231,7 @@ func messageHandler(client mqtt.Client, msg mqtt.Message) {
_, err = db.Exec(insertQuery, args...)
if err != nil {
publishError(client, errorTopic, fmt.Sprintf("❌ Failed to insert into mfm_readings: %v", err))
logAndPublishError(client, errorTopic, fmt.Sprintf("❌ Failed to insert into mfm_readings: %v", err))
return
}
@@ -241,7 +241,7 @@ func messageHandler(client mqtt.Client, msg mqtt.Message) {
// Publishes error messages to a specific topic
func publishError(client mqtt.Client, topic, message string) {
func logAndPublishError(client mqtt.Client, topic, message string) {
token := client.Publish(topic, 1, false, message)
token.Wait()
if token.Error() != nil {
@@ -303,6 +303,10 @@ func main() {
sigChan := make(chan os.Signal, 1)
<-sigChan
}
func logAndPublishError(client mqtt.Client, errorTopic string, message string) {
log.Printf("[ERROR PUBLISH] Topic: %s | Message: %s", errorTopic, message)
publishError(client, errorTopic, message)
}