Skip to content

Commit

Permalink
added QOL responses to incorrect API requests in rpc module
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Nguyen committed Feb 19, 2019
1 parent 191b621 commit 95e25a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 6 additions & 1 deletion rpc/relay/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
}
Expand Down
11 changes: 10 additions & 1 deletion rpc/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit 95e25a8

Please sign in to comment.