From b4143ed5895e276a045ac601327acd3223997544 Mon Sep 17 00:00:00 2001 From: Mysh! Date: Tue, 9 Mar 2021 09:55:22 +0800 Subject: [PATCH] Panic fix (#44) This fixes the bug when there are no hypha in the wiki and the user clicks the "random" button. Since there are zero amount of pages, a number 0 is given to random, which causes a panic and resets the connection. See: https://github.com/bouncepaw/mycorrhiza/issues/44 --- main.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index f8780b1..203c59d 100644 --- a/main.go +++ b/main.go @@ -85,8 +85,16 @@ func handlerUpdateHeaderLinks(w http.ResponseWriter, rq *http.Request) { // Redirect to a random hypha. func handlerRandom(w http.ResponseWriter, rq *http.Request) { log.Println(rq.URL) - var randomHyphaName string - i := rand.Intn(hyphae.Count()) + var ( + randomHyphaName string + amountOfHyphae int = hyphae.Count() + ) + if amountOfHyphae == 0 { + HttpErr(w, http.StatusNotFound, util.HomePage, "There are no hyphae", + "It is not possible to display a random hypha because the wiki does not contain any hyphae") + return + } + i := rand.Intn(amountOfHyphae) for h := range hyphae.YieldExistingHyphae() { if i == 0 { randomHyphaName = h.Name