From 4bcfa80dc475adaa6bba6e45ae0de3afb7420bf6 Mon Sep 17 00:00:00 2001 From: jholdstock Date: Mon, 3 Aug 2020 16:17:19 +0100 Subject: [PATCH] Fix admin page load when no wallet clients connected. --- webapi/middleware.go | 10 ++++------ webapi/setvotechoices.go | 7 +++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/webapi/middleware.go b/webapi/middleware.go index 8cb67cd..9866d5c 100644 --- a/webapi/middleware.go +++ b/webapi/middleware.go @@ -90,17 +90,15 @@ func withDcrdClient(dcrd rpc.DcrdConnect) gin.HandlerFunc { } } -// withWalletClients middleware adds a voting wallet clients to the request -// context for downstream handlers to make use of. +// withWalletClients middleware attempts to add voting wallet clients to the +// request context for downstream handlers to make use of. Downstream handlers +// must handle the case where no wallet clients are connected. func withWalletClients(wallets rpc.WalletConnect) gin.HandlerFunc { return func(c *gin.Context) { clients, failedConnections := wallets.Clients(c, cfg.NetParams) if len(clients) == 0 { log.Error("Could not connect to any wallets") - sendError(errInternalError, c) - return - } - if len(failedConnections) > 0 { + } else if len(failedConnections) > 0 { log.Errorf("Failed to connect to %d wallet(s), proceeding with only %d", len(failedConnections), len(clients)) } diff --git a/webapi/setvotechoices.go b/webapi/setvotechoices.go index 3f5c4f8..095cf7c 100644 --- a/webapi/setvotechoices.go +++ b/webapi/setvotechoices.go @@ -17,6 +17,13 @@ func setVoteChoices(c *gin.Context) { knownTicket := c.MustGet("KnownTicket").(bool) walletClients := c.MustGet("WalletClients").([]*rpc.WalletRPC) + // If we cannot set the vote choices on at least one voting wallet right + // now, don't update the database, just return an error. + if len(walletClients) == 0 { + sendError(errInternalError, c) + return + } + if !knownTicket { log.Warnf("%s: Unknown ticket (clientIP=%s)", funcName, c.ClientIP()) sendError(errUnknownTicket, c)