mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-23 03:38:35 +00:00
rpc: get rid of erverError type
Retrieve error's HTTP code in a separate method.
This commit is contained in:
parent
6434404081
commit
ea6ddbee5b
2 changed files with 8 additions and 17 deletions
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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:
|
||||
|
@ -59,8 +53,5 @@ func packClientError(respErr *response.Error) *serverError {
|
|||
default:
|
||||
httpCode = http.StatusUnprocessableEntity
|
||||
}
|
||||
return &serverError{
|
||||
Error: respErr,
|
||||
HTTPCode: httpCode,
|
||||
}
|
||||
return httpCode
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue