Delay dcrd client error handling.

Errors generated during dcrd client creation were previously handled in middleware, and request handling was terminated immediately.

Now the error handling is delayed by adding the error details to the request context. This enables downstream handlers to decide what to do with the error.
This commit is contained in:
jholdstock 2021-11-03 10:35:45 +00:00 committed by Jamie Holdstock
parent ce5227356d
commit d337e0a321
5 changed files with 47 additions and 6 deletions

View File

@ -76,6 +76,12 @@ func feeAddress(c *gin.Context) {
knownTicket := c.MustGet("KnownTicket").(bool)
commitmentAddress := c.MustGet("CommitmentAddress").(string)
dcrdClient := c.MustGet("DcrdClient").(*rpc.DcrdRPC)
dcrdErr := c.MustGet("DcrdClientErr")
if dcrdErr != nil {
log.Errorf("%s: could not get dcrd client: %v", funcName, dcrdErr.(error))
sendError(errInternalError, c)
return
}
reqBytes := c.MustGet("RequestBytes").([]byte)
if cfg.VspClosed {

View File

@ -76,13 +76,10 @@ func requireAdmin() gin.HandlerFunc {
func withDcrdClient(dcrd rpc.DcrdConnect) gin.HandlerFunc {
return func(c *gin.Context) {
client, err := dcrd.Client(c, cfg.NetParams)
if err != nil {
log.Error(err)
sendError(errInternalError, c)
return
}
// Don't handle the error here, add it to the context and let downstream
// handlers decide what to do with it.
c.Set("DcrdClient", client)
c.Set("DcrdClientErr", err)
}
}
@ -179,6 +176,13 @@ func broadcastTicket() gin.HandlerFunc {
// Check if local dcrd already knows the parent tx.
dcrdClient := c.MustGet("DcrdClient").(*rpc.DcrdRPC)
dcrdErr := c.MustGet("DcrdClientErr")
if dcrdErr != nil {
log.Errorf("%s: could not get dcrd client: %v", funcName, dcrdErr.(error))
sendError(errInternalError, c)
return
}
_, err = dcrdClient.GetRawTransaction(parentHash.String())
var e *wsrpc.Error
if err == nil {
@ -312,6 +316,12 @@ func vspAuth() gin.HandlerFunc {
commitmentAddress = ticket.CommitmentAddress
} else {
dcrdClient := c.MustGet("DcrdClient").(*rpc.DcrdRPC)
dcrdErr := c.MustGet("DcrdClientErr")
if dcrdErr != nil {
log.Errorf("%s: could not get dcrd client: %v", funcName, dcrdErr.(error))
sendError(errInternalError, c)
return
}
resp, err := dcrdClient.GetRawTransaction(hash)
if err != nil {

View File

@ -27,6 +27,12 @@ func payFee(c *gin.Context) {
ticket := c.MustGet("Ticket").(database.Ticket)
knownTicket := c.MustGet("KnownTicket").(bool)
dcrdClient := c.MustGet("DcrdClient").(*rpc.DcrdRPC)
dcrdErr := c.MustGet("DcrdClientErr")
if dcrdErr != nil {
log.Errorf("%s: could not get dcrd client: %v", funcName, dcrdErr.(error))
sendError(errInternalError, c)
return
}
reqBytes := c.MustGet("RequestBytes").([]byte)
if cfg.VspClosed {

View File

@ -32,6 +32,12 @@ func setAltSig(c *gin.Context) {
// Get values which have been added to context by middleware.
dcrdClient := c.MustGet("DcrdClient").(Node)
dcrdErr := c.MustGet("DcrdClientErr")
if dcrdErr != nil {
log.Errorf("%s: could not get dcrd client: %v", funcName, dcrdErr.(error))
sendError(errInternalError, c)
return
}
reqBytes := c.MustGet("RequestBytes").([]byte)
if cfg.VspClosed {

View File

@ -119,6 +119,7 @@ func TestSetAltSig(t *testing.T) {
const testAddr = "DsVoDXNQqyF3V83PJJ5zMdnB4pQuJHBAh15"
tests := []struct {
name string
dcrdClientErr bool
vspClosed bool
deformReq int
addr string
@ -136,6 +137,11 @@ func TestSetAltSig(t *testing.T) {
vspClosed: true,
wantCode: http.StatusBadRequest,
}, {
name: "dcrd client error",
dcrdClientErr: true,
wantCode: http.StatusInternalServerError,
}, {
name: "bad request",
deformReq: 1,
wantCode: http.StatusBadRequest,
@ -207,8 +213,15 @@ func TestSetAltSig(t *testing.T) {
w := httptest.NewRecorder()
c, r := gin.CreateTestContext(w)
var dcrdErr error
if test.dcrdClientErr {
tNode = nil
dcrdErr = errors.New("error")
}
handle := func(c *gin.Context) {
c.Set("DcrdClient", tNode)
c.Set("DcrdClientErr", dcrdErr)
c.Set("RequestBytes", b[test.deformReq:])
setAltSig(c)
}