From 95e25a83df083b4b1f63f47d8adf3810844651a2 Mon Sep 17 00:00:00 2001 From: Andrew Nguyen Date: Tue, 19 Feb 2019 12:25:11 -0500 Subject: [PATCH] added QOL responses to incorrect API requests in rpc module --- rpc/relay/dispatch.go | 7 ++++++- rpc/relay/relay.go | 11 ++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/rpc/relay/dispatch.go b/rpc/relay/dispatch.go index 9303c3c93..438fcad5d 100644 --- a/rpc/relay/dispatch.go +++ b/rpc/relay/dispatch.go @@ -3,7 +3,7 @@ package relay import ( "net/http" - + "github.com/julienschmidt/httprouter" "github.com/pokt-network/pocket-core/dispatch" "github.com/pokt-network/pocket-core/logs" @@ -15,6 +15,11 @@ func Dispatch(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { d := &dispatch.Dispatch{} if err := shared.PopModel(w, r, ps, d); err != nil { logs.NewLog(err.Error(), logs.ErrorLevel, logs.JSONLogFormat) + shared.WriteErrorResponse(w, 400, err.Error()) + return + } + if d.DevID=="" || len(d.Blockchains)== 0 { + shared.WriteErrorResponse(w, 400, "Request was not formatted properly") } shared.WriteRawJSONResponse(w, dispatch.Serve(d)) } diff --git a/rpc/relay/relay.go b/rpc/relay/relay.go index 8fdd51071..9a4643281 100644 --- a/rpc/relay/relay.go +++ b/rpc/relay/relay.go @@ -2,7 +2,7 @@ package relay import ( "net/http" - + "github.com/julienschmidt/httprouter" "github.com/pokt-network/pocket-core/logs" "github.com/pokt-network/pocket-core/rpc/shared" @@ -14,10 +14,17 @@ func Relay(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { relay := &service.Relay{} if err := shared.PopModel(w, r, ps, relay); err != nil { logs.NewLog(err.Error(), logs.ErrorLevel, logs.JSONLogFormat) + shared.WriteErrorResponse(w, 400, err.Error()) + return + } + if relay.Blockchain == "" || relay.NetworkID == "" || relay.DevID == "" || relay.Version == "" || relay.Data == "" { + shared.WriteErrorResponse(w, 400, "The request was not properly formatted") + return } response, err := service.RouteRelay(*relay) if err != nil { logs.NewLog(err.Error(), logs.ErrorLevel, logs.JSONLogFormat) + // error is embedded in the response } shared.WriteJSONResponse(w, response) // relay the response } @@ -35,6 +42,8 @@ func Report(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { report := &service.Report{} if err := shared.PopModel(w, r, ps, report); err != nil { logs.NewLog(err.Error(), logs.ErrorLevel, logs.JSONLogFormat) + shared.WriteErrorResponse(w, 400, err.Error()) + return } response, err := service.HandleReport(report) if err != nil {