Files
backend/watchdog/processor.go
NanamiAdmin 1432651a14 fix(watchdog): fix watchdog unable to send webhook and simplify the parsing of json config
- Change webhook config headers and body from map to string format
- Move command parsing logic to separate function in command.go
- Add debug logging for webhook operations
- Improve exception handling with proper JSON parsing
- Simplify config loading logic with default values
2026-04-28 20:48:54 +08:00

62 lines
1.9 KiB
Go

package watchdog
import (
"encoding/json"
"fmt"
"strings"
"super-frpc/global"
"super-frpc/postLog"
"super-frpc/webhook"
)
func exceptionHandle(exceptionType string, serviceName string, errorMsg string) {
if global.CurrentConfig.Notification.Enabled {
if global.CurrentConfig.Notification.Method == "webhook" {
var bodyMap map[string]string
err := json.Unmarshal([]byte(global.CurrentConfig.Webhook.Body), &bodyMap)
if err != nil {
postLog.Error(fmt.Sprintf("[exceptionHandler] Failed to parse webhook body: %v", err))
return
}
for k, v := range bodyMap {
replaced := v
for strings.Contains(replaced, "{{ exceptionType }}") ||
strings.Contains(replaced, "{{ serviceName }}") ||
strings.Contains(replaced, "{{ exceptionMsg }}") {
replaced = strings.ReplaceAll(replaced, "{{ exceptionType }}", exceptionType)
replaced = strings.ReplaceAll(replaced, "{{ serviceName }}", serviceName)
replaced = strings.ReplaceAll(replaced, "{{ exceptionMsg }}", errorMsg)
}
bodyMap[k] = replaced
}
bodyJSON, err := json.Marshal(bodyMap)
if err != nil {
postLog.Error(fmt.Sprintf("[exceptionHandler] Failed to marshal webhook body: %v", err))
return
}
// Parse headers as map[string]string format
headersMap := make(map[string]string)
headerPairs := strings.Split(global.CurrentConfig.Webhook.Headers, ";")
for _, pair := range headerPairs {
pair = strings.TrimSpace(pair)
if pair == "" {
continue
}
parts := strings.SplitN(pair, ":", 2)
if len(parts) == 2 {
headersMap[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
}
}
webhook.SendHook(global.CurrentConfig.Webhook.URL, global.CurrentConfig.Webhook.Method, headersMap, string(bodyJSON))
}
} else {
postLog.Warning("[exceptionHandler] exception occoured, but notification is not enabled!")
}
}