1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-03-13 23:18:17 +00:00

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
This commit is contained in:
Mysh! 2021-03-09 09:55:22 +08:00
parent df9ee15a7f
commit b4143ed589
No known key found for this signature in database
GPG Key ID: E2C8CCA3392D75BD

12
main.go
View File

@ -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