rpc: unexport ServerError

It's internal server thing that is not for the outside users.
This commit is contained in:
Anna Shaleva 2022-06-09 18:19:01 +03:00
parent 0687924e87
commit ef9eca7cce
4 changed files with 184 additions and 148 deletions

66
pkg/rpc/server/error.go Normal file
View file

@ -0,0 +1,66 @@
package server
import (
"net/http"
"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 {
RunForErrors(f func(jsonErr *response.Error))
}
// abstract represents abstract JSON-RPC 2.0 response. It is used as a server-side response
// representation.
type abstract struct {
response.Header
Error *serverError `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)
}
}
// abstractBatch represents abstract JSON-RPC 2.0 batch-response.
type abstractBatch []abstract
// RunForErrors implements abstractResult interface.
func (ab abstractBatch) RunForErrors(f func(jsonErr *response.Error)) {
for _, a := range ab {
if a.Error != nil {
f(a.Error.Error)
}
}
}
func packClientError(respErr *response.Error) *serverError {
var httpCode int
switch respErr.Code {
case response.BadRequestCode:
httpCode = http.StatusBadRequest
case response.InvalidRequestCode, response.RPCErrorCode, response.InvalidParamsCode:
httpCode = http.StatusUnprocessableEntity
case response.MethodNotFoundCode:
httpCode = http.StatusMethodNotAllowed
case response.InternalServerErrorCode:
httpCode = http.StatusInternalServerError
default:
httpCode = http.StatusUnprocessableEntity
}
return &serverError{
Error: respErr,
HTTPCode: httpCode,
}
}