diff options
-rw-r--r-- | main.go | 25 |
1 files changed, 18 insertions, 7 deletions
@@ -69,6 +69,10 @@ var ( Name: "serie_count", Help: "How often a serie is called", }, []string{"serie"}) + panicCount = promauto.NewCounter(prometheus.CounterOpts{ + Name: "panic", + Help: "How many times the application panic", + }) ) func getSeries(r *http.Request) []string { @@ -125,8 +129,10 @@ func fetchXML(_ context.Context) ([]byte, error) { } func appendTag(tag *etree.Element, ap string) { - text := tag.Text() - tag.SetText(text + ap) + if tag != nil { + text := tag.Text() + tag.SetText(text + ap) + } } func filterBySeries(series []string, xml []byte, temper bool) ([]byte, error) { @@ -163,6 +169,14 @@ func filterBySeries(series []string, xml []byte, temper bool) ([]byte, error) { func handleError(next errorRequestHandler) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + defer func() { + if perr := recover(); perr != nil { + w.WriteHeader(http.StatusInternalServerError) + slog.Error("Request panic", "error", perr) + panicCount.Inc() + } + }() + if err := next(w, r); err != nil { slog.ErrorContext(r.Context(), "Error", "error", err.Error()) @@ -242,10 +256,8 @@ func view(w http.ResponseWriter, r *http.Request) error { } func podcast(w http.ResponseWriter, r *http.Request) error { - if r.URL.Path != "/" { return errNotFound - } xml, err := fetchXML(r.Context()) @@ -306,9 +318,7 @@ func main() { return } - var ( - addr = flag.String("addr", ":8080", "Server address") - ) + addr := flag.String("addr", ":8080", "Server address") flag.Parse() @@ -323,6 +333,7 @@ func main() { Addr: *addr, } + slog.Info("Starting server", "addr", *addr) err := server.ListenAndServe() if err != nil { fmt.Printf("Server error: %s", err.Error()) |