2022-07-21 13:21:44 +00:00
|
|
|
package rpcsrv
|
2022-06-09 15:19:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2022-07-22 16:09:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc"
|
2022-06-09 15:19:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// abstractResult is an interface which represents either single JSON-RPC 2.0 response
|
|
|
|
// or batch JSON-RPC 2.0 response.
|
|
|
|
type abstractResult interface {
|
2022-07-22 16:09:29 +00:00
|
|
|
RunForErrors(f func(jsonErr *neorpc.Error))
|
2022-06-09 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// abstract represents abstract JSON-RPC 2.0 response. It is used as a server-side response
|
|
|
|
// representation.
|
|
|
|
type abstract struct {
|
2022-07-22 16:09:29 +00:00
|
|
|
neorpc.Header
|
|
|
|
Error *neorpc.Error `json:"error,omitempty"`
|
|
|
|
Result interface{} `json:"result,omitempty"`
|
2022-06-09 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunForErrors implements abstractResult interface.
|
2022-07-22 16:09:29 +00:00
|
|
|
func (a abstract) RunForErrors(f func(jsonErr *neorpc.Error)) {
|
2022-06-09 15:19:01 +00:00
|
|
|
if a.Error != nil {
|
2022-06-10 13:26:33 +00:00
|
|
|
f(a.Error)
|
2022-06-09 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// abstractBatch represents abstract JSON-RPC 2.0 batch-response.
|
|
|
|
type abstractBatch []abstract
|
|
|
|
|
|
|
|
// RunForErrors implements abstractResult interface.
|
2022-07-22 16:09:29 +00:00
|
|
|
func (ab abstractBatch) RunForErrors(f func(jsonErr *neorpc.Error)) {
|
2022-06-09 15:19:01 +00:00
|
|
|
for _, a := range ab {
|
|
|
|
if a.Error != nil {
|
2022-06-10 13:26:33 +00:00
|
|
|
f(a.Error)
|
2022-06-09 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 16:09:29 +00:00
|
|
|
func getHTTPCodeForError(respErr *neorpc.Error) int {
|
2022-06-09 15:19:01 +00:00
|
|
|
var httpCode int
|
|
|
|
switch respErr.Code {
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.BadRequestCode:
|
2022-06-09 15:19:01 +00:00
|
|
|
httpCode = http.StatusBadRequest
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.InvalidRequestCode, neorpc.RPCErrorCode, neorpc.InvalidParamsCode:
|
2022-06-09 15:19:01 +00:00
|
|
|
httpCode = http.StatusUnprocessableEntity
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.MethodNotFoundCode:
|
2022-06-09 15:19:01 +00:00
|
|
|
httpCode = http.StatusMethodNotAllowed
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.InternalServerErrorCode:
|
2022-06-09 15:19:01 +00:00
|
|
|
httpCode = http.StatusInternalServerError
|
|
|
|
default:
|
|
|
|
httpCode = http.StatusUnprocessableEntity
|
|
|
|
}
|
2022-06-10 13:26:33 +00:00
|
|
|
return httpCode
|
2022-06-09 15:19:01 +00:00
|
|
|
}
|