From b45aa84b1d8dc8d2f839049df8d67efee7ed2436 Mon Sep 17 00:00:00 2001 From: Sam Willcocks Date: Thu, 4 Aug 2022 02:04:16 +0300 Subject: [PATCH] Handle non-existent screenname gracefully --- main.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index b177247..3fd0c2a 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,14 @@ func probeTwitter(ctx context.Context, target string, registry *prometheus.Regis log.Errorf("decoding json: %s", err) return false } + switch l := len(data); { + case l == 0: + log.Errorf("No data returned. Does screen name exist?") + return false + case l > 1: + log.Errorf("Unexpected (>1) results returned. WTF twitter??") + return false + } followersGauge.With(prometheus.Labels{"screen_name": target}).Set(float64(data[0].FollowersCount)) return true } @@ -60,6 +68,13 @@ func twitterHandler(w http.ResponseWriter, r *http.Request) { h.ServeHTTP(w, r) } + +func bail(err error) { + if err != nil { + log.Fatal(err) + } +} + func main() { var listenAddr string flag.StringVar(&listenAddr, "listen", "0.0.0.0:9700", "Address to listen on") @@ -69,5 +84,5 @@ func main() { twitterHandler(w, r) }) log.Infof("Listening on %s", listenAddr) - http.ListenAndServe(listenAddr, nil) + bail(http.ListenAndServe(listenAddr, nil)) }