Skip to content

Commit

Permalink
rpc: get rid of erverError type
Browse files Browse the repository at this point in the history
Retrieve error's HTTP code in a separate method.
  • Loading branch information
AnnaShaleva committed Jun 10, 2022
1 parent 6434404 commit ea6ddbe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
21 changes: 6 additions & 15 deletions pkg/rpc/server/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/rpc/response"
)

// serverError represents RPC error response on the server side.
type serverError struct {
*response.Error
HTTPCode int // HTTPCode won't be marshalled because Error's marshaller is used.
}

// abstractResult is an interface which represents either single JSON-RPC 2.0 response
// or batch JSON-RPC 2.0 response.
type abstractResult interface {
Expand All @@ -22,14 +16,14 @@ type abstractResult interface {
// representation.
type abstract struct {
response.Header
Error *serverError `json:"error,omitempty"`
Result interface{} `json:"result,omitempty"`
Error *response.Error `json:"error,omitempty"`
Result interface{} `json:"result,omitempty"`
}

// RunForErrors implements abstractResult interface.
func (a abstract) RunForErrors(f func(jsonErr *response.Error)) {
if a.Error != nil {
f(a.Error.Error)
f(a.Error)
}
}

Expand All @@ -40,12 +34,12 @@ type abstractBatch []abstract
func (ab abstractBatch) RunForErrors(f func(jsonErr *response.Error)) {
for _, a := range ab {
if a.Error != nil {
f(a.Error.Error)
f(a.Error)
}
}
}

func packClientError(respErr *response.Error) *serverError {
func getHTTPCodeForError(respErr *response.Error) int {
var httpCode int
switch respErr.Code {
case response.BadRequestCode:
Expand All @@ -59,8 +53,5 @@ func packClientError(respErr *response.Error) *serverError {
default:
httpCode = http.StatusUnprocessableEntity
}
return &serverError{
Error: respErr,
HTTPCode: httpCode,
}
return httpCode
}
4 changes: 2 additions & 2 deletions pkg/rpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2250,7 +2250,7 @@ func (s *Server) packResponse(r *request.In, result interface{}, respErr *respon
},
}
if respErr != nil {
resp.Error = packClientError(respErr)
resp.Error = respErr
} else {
resp.Result = result
}
Expand Down Expand Up @@ -2292,7 +2292,7 @@ func (s *Server) writeHTTPServerResponse(r *request.Request, w http.ResponseWrit
if r.In != nil {
resp := resp.(abstract)
if resp.Error != nil {
w.WriteHeader(resp.Error.HTTPCode)
w.WriteHeader(getHTTPCodeForError(resp.Error))
}
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
Expand Down

0 comments on commit ea6ddbe

Please sign in to comment.