2020-01-14 12:02:38 +00:00
|
|
|
package response
|
2018-03-23 20:36:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2022-06-09 15:19:01 +00:00
|
|
|
// Error represents JSON-RPC 2.0 error type.
|
|
|
|
type Error struct {
|
|
|
|
Code int64 `json:"code"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Data string `json:"data,omitempty"`
|
|
|
|
}
|
2022-06-08 15:14:00 +00:00
|
|
|
|
2022-06-09 15:19:01 +00:00
|
|
|
// Standard RPC error codes defined by the JSON-RPC 2.0 specification.
|
|
|
|
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
|
2018-03-23 20:36:59 +00:00
|
|
|
)
|
|
|
|
|
2022-06-09 15:19:01 +00:00
|
|
|
// RPC error codes defined by the Neo JSON-RPC specification extension.
|
|
|
|
const (
|
|
|
|
// RPCErrorCode is returned on RPC request processing error.
|
|
|
|
RPCErrorCode = -100
|
|
|
|
)
|
2022-05-16 09:48:08 +00:00
|
|
|
|
2019-02-13 18:01:52 +00:00
|
|
|
var (
|
2020-01-14 12:02:38 +00:00
|
|
|
// ErrInvalidParams represents a generic 'invalid parameters' error.
|
2022-06-07 09:27:58 +00:00
|
|
|
ErrInvalidParams = NewInvalidParamsError("invalid params")
|
2021-05-12 20:17:03 +00:00
|
|
|
// ErrAlreadyExists represents SubmitError with code -501.
|
2020-03-02 17:01:32 +00:00
|
|
|
ErrAlreadyExists = NewSubmitError(-501, "Block or transaction already exists and cannot be sent repeatedly.")
|
2021-05-12 20:17:03 +00:00
|
|
|
// ErrOutOfMemory represents SubmitError with code -502.
|
2020-03-02 17:01:32 +00:00
|
|
|
ErrOutOfMemory = NewSubmitError(-502, "The memory pool is full and no more transactions can be sent.")
|
2021-05-12 20:17:03 +00:00
|
|
|
// ErrUnableToVerify represents SubmitError with code -503.
|
2020-03-02 17:01:32 +00:00
|
|
|
ErrUnableToVerify = NewSubmitError(-503, "The block cannot be validated.")
|
2021-05-12 20:17:03 +00:00
|
|
|
// ErrValidationFailed represents SubmitError with code -504.
|
2020-03-02 17:01:32 +00:00
|
|
|
ErrValidationFailed = NewSubmitError(-504, "Block or transaction validation failed.")
|
2021-05-12 20:17:03 +00:00
|
|
|
// ErrPolicyFail represents SubmitError with code -505.
|
2020-03-02 17:01:32 +00:00
|
|
|
ErrPolicyFail = NewSubmitError(-505, "One of the Policy filters failed.")
|
2021-05-12 20:17:03 +00:00
|
|
|
// ErrUnknown represents SubmitError with code -500.
|
2020-03-02 17:01:32 +00:00
|
|
|
ErrUnknown = NewSubmitError(-500, "Unknown error.")
|
2019-02-13 18:01:52 +00:00
|
|
|
)
|
|
|
|
|
2022-06-09 15:19:01 +00:00
|
|
|
// NewError is an Error constructor that takes Error contents from its parameters.
|
|
|
|
func NewError(code int64, message string, data string) *Error {
|
|
|
|
return &Error{
|
|
|
|
Code: code,
|
|
|
|
Message: message,
|
|
|
|
Data: data,
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewParseError creates a new error with code
|
2021-05-12 20:17:03 +00:00
|
|
|
// -32700.
|
2022-06-09 15:19:01 +00:00
|
|
|
func NewParseError(data string) *Error {
|
|
|
|
return NewError(BadRequestCode, "Parse Error", data)
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewInvalidRequestError creates a new error with
|
|
|
|
// code -32600.
|
2022-06-09 15:19:01 +00:00
|
|
|
func NewInvalidRequestError(data string) *Error {
|
|
|
|
return NewError(InvalidRequestCode, "Invalid Request", data)
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMethodNotFoundError creates a new error with
|
|
|
|
// code -32601.
|
2022-06-09 15:19:01 +00:00
|
|
|
func NewMethodNotFoundError(data string) *Error {
|
|
|
|
return NewError(MethodNotFoundCode, "Method not found", data)
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewInvalidParamsError creates a new error with
|
|
|
|
// code -32602.
|
2022-06-09 15:19:01 +00:00
|
|
|
func NewInvalidParamsError(data string) *Error {
|
|
|
|
return NewError(InvalidParamsCode, "Invalid Params", data)
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewInternalServerError creates a new error with
|
|
|
|
// code -32603.
|
2022-06-09 15:19:01 +00:00
|
|
|
func NewInternalServerError(data string) *Error {
|
|
|
|
return NewError(InternalServerErrorCode, "Internal error", data)
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 16:06:34 +00:00
|
|
|
// NewRPCError creates a new error with
|
2021-05-12 20:17:03 +00:00
|
|
|
// code -100.
|
2022-06-09 15:19:01 +00:00
|
|
|
func NewRPCError(message string, data string) *Error {
|
|
|
|
return NewError(RPCErrorCode, message, data)
|
2020-02-15 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 17:01:32 +00:00
|
|
|
// NewSubmitError creates a new error with
|
2021-05-12 20:17:03 +00:00
|
|
|
// specified error code and error message.
|
2022-06-09 15:19:01 +00:00
|
|
|
func NewSubmitError(code int64, message string) *Error {
|
|
|
|
return NewError(code, message, "")
|
2022-06-08 15:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WrapErrorWithData returns copy of the given error with the specified data and cause.
|
|
|
|
// It does not modify the source error.
|
2022-06-09 15:19:01 +00:00
|
|
|
func WrapErrorWithData(e *Error, data string) *Error {
|
|
|
|
return NewError(e.Code, e.Message, data)
|
2020-03-02 17:01:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// Error implements the error interface.
|
2020-01-14 12:02:38 +00:00
|
|
|
func (e *Error) Error() string {
|
2022-06-07 09:27:58 +00:00
|
|
|
if len(e.Data) == 0 {
|
|
|
|
return fmt.Sprintf("%s (%d)", e.Message, e.Code)
|
2021-09-27 12:37:29 +00:00
|
|
|
}
|
2022-06-07 09:27:58 +00:00
|
|
|
return fmt.Sprintf("%s (%d) - %s", e.Message, e.Code, e.Data)
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
2021-02-25 09:08:41 +00:00
|
|
|
|
2022-06-08 15:14:00 +00:00
|
|
|
// Is denotes whether the error matches the target one.
|
|
|
|
func (e *Error) Is(target error) bool {
|
|
|
|
clTarget, ok := target.(*Error)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return e.Code == clTarget.Code
|
2021-02-25 09:08:41 +00:00
|
|
|
}
|