Skip to content

Commit

Permalink
rpc: unexport ServerError
Browse files Browse the repository at this point in the history
It's internal server thing that is not for the outside users.
  • Loading branch information
AnnaShaleva committed Jun 9, 2022
1 parent 7481b59 commit 5d8a5f5
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 147 deletions.
81 changes: 41 additions & 40 deletions pkg/rpc/response/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ package response

import (
"fmt"
"net/http"
)

type (
// ServerError object for outputting JSON-RPC 2.0 errors on the server side.
ServerError struct {
*Error
HTTPCode int // HTTPCode won't be marshalled because Error's marshaller is used.
}
// Error object for outputting JSON-RPC 2.0 errors on the client side.
type Error struct {
Code int64 `json:"code"`
Message string `json:"message"`
Data string `json:"data,omitempty"`
}

// Error object for outputting JSON-RPC 2.0 errors on the client side.
Error struct {
Code int64 `json:"code"`
Message string `json:"message"`
Data string `json:"data,omitempty"`
}
// Various RPC error codes.
const (
// InternalServerErrorCode is returned for internal RPC server error.
InternalServerErrorCode = -32603
// BadRequestCode is returned on parse error.
BadRequestCode = -32700
// InvalidRequestCode is returned on invalid request.
InvalidRequestCode = -32600
// MethodNotFoundCode is returned on unknown method calling.
MethodNotFoundCode = -32601
// InvalidParamsCode is returned on request with invalid params.
InvalidParamsCode = -32602
// RPCErrorCode is returned on RPC request processing error.
RPCErrorCode = -100
)

// InternalServerErrorCode is returned for internal RPC server error.
const InternalServerErrorCode = -32603

var (
// ErrInvalidParams represents a generic 'invalid parameters' error.
ErrInvalidParams = NewInvalidParamsError("invalid params")
Expand All @@ -42,63 +46,60 @@ var (

// NewServerError is an ServerError constructor that takes ServerError contents from its
// parameters.
func NewServerError(code int64, httpCode int, message string, data string) *ServerError {
return &ServerError{
Error: &Error{
Code: code,
Message: message,
Data: data,
},
HTTPCode: httpCode,
func NewServerError(code int64, message string, data string) *Error {
return &Error{
Code: code,
Message: message,
Data: data,
}
}

// NewParseError creates a new error with code
// -32700.
func NewParseError(data string) *ServerError {
return NewServerError(-32700, http.StatusBadRequest, "Parse Error", data)
func NewParseError(data string) *Error {
return NewServerError(BadRequestCode, "Parse Error", data)
}

// NewInvalidRequestError creates a new error with
// code -32600.
func NewInvalidRequestError(data string) *ServerError {
return NewServerError(-32600, http.StatusUnprocessableEntity, "Invalid Request", data)
func NewInvalidRequestError(data string) *Error {
return NewServerError(InvalidRequestCode, "Invalid Request", data)
}

// NewMethodNotFoundError creates a new error with
// code -32601.
func NewMethodNotFoundError(data string) *ServerError {
return NewServerError(-32601, http.StatusMethodNotAllowed, "Method not found", data)
func NewMethodNotFoundError(data string) *Error {
return NewServerError(MethodNotFoundCode, "Method not found", data)
}

// NewInvalidParamsError creates a new error with
// code -32602.
func NewInvalidParamsError(data string) *ServerError {
return NewServerError(-32602, http.StatusUnprocessableEntity, "Invalid Params", data)
func NewInvalidParamsError(data string) *Error {
return NewServerError(InvalidParamsCode, "Invalid Params", data)
}

// NewInternalServerError creates a new error with
// code -32603.
func NewInternalServerError(data string) *ServerError {
return NewServerError(InternalServerErrorCode, http.StatusInternalServerError, "Internal error", data)
func NewInternalServerError(data string) *Error {
return NewServerError(InternalServerErrorCode, "Internal error", data)
}

// NewRPCError creates a new error with
// code -100.
func NewRPCError(message string, data string) *ServerError {
return NewServerError(-100, http.StatusUnprocessableEntity, message, data)
func NewRPCError(message string, data string) *Error {
return NewServerError(RPCErrorCode, message, data)
}

// NewSubmitError creates a new error with
// specified error code and error message.
func NewSubmitError(code int64, message string) *ServerError {
return NewServerError(code, http.StatusUnprocessableEntity, message, "")
func NewSubmitError(code int64, message string) *Error {
return NewServerError(code, message, "")
}

// WrapErrorWithData returns copy of the given error with the specified data and cause.
// It does not modify the source error.
func WrapErrorWithData(e *ServerError, data string) *ServerError {
return NewServerError(e.Code, e.HTTPCode, e.Message, data)
func WrapErrorWithData(e *Error, data string) *Error {
return NewServerError(e.Code, e.Message, data)
}

// Error implements the error interface.
Expand Down
34 changes: 0 additions & 34 deletions pkg/rpc/response/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,6 @@ type Raw struct {
Result json.RawMessage `json:"result,omitempty"`
}

// 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 *Error))
}

// Abstract represents abstract JSON-RPC 2.0 response, it differs from Raw in
// that Result field is an interface here. It is used as a server-side response
// representation.
type Abstract struct {
Header
Error *ServerError `json:"error,omitempty"`
Result interface{} `json:"result,omitempty"`
}

// RunForErrors implements AbstractResult interface.
func (a Abstract) RunForErrors(f func(jsonErr *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 *Error)) {
for _, a := range ab {
if a.Error != nil {
f(a.Error.Error)
}
}
}

// Notification is a type used to represent wire format of events, they're
// special in that they look like requests but they don't have IDs and their
// "method" is actually an event name.
Expand Down
67 changes: 67 additions & 0 deletions pkg/rpc/server/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 differs from Raw in
// that Result field is an interface here. 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,
}
}
Loading

0 comments on commit 5d8a5f5

Please sign in to comment.