jsonrpc

package
v0.79.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2024 License: AGPL-3.0 Imports: 6 Imported by: 1

Documentation

Index

Constants

View Source
const (
	VERSION2   string  = "2.0"
	TraceIDKey TraceID = "trace-id"
)

Variables

View Source
var (
	ErrOnlySupportJSONRPC2 = errors.New("the API only supports JSON-RPC 2.0")
	ErrMethodIsRequired    = errors.New("the method is required")
)

Functions

func TraceIDFromContext added in v0.67.0

func TraceIDFromContext(ctx context.Context) string

Types

type Command

type Command interface {
	Handle(ctx context.Context, params Params) (Result, *ErrorDetails)
}

type Dispatcher added in v0.67.0

type Dispatcher struct {
	// contains filtered or unexported fields
}

Dispatcher forward the request it gets in input to the associated method. Despite being useful for simple use cases, it may not fit more advanced use cases.

func NewDispatcher added in v0.67.0

func NewDispatcher(log *zap.Logger) *Dispatcher

func (*Dispatcher) AddInterceptor added in v0.67.0

func (a *Dispatcher) AddInterceptor(interceptor InterceptorFunc)

func (*Dispatcher) DispatchRequest added in v0.67.0

func (a *Dispatcher) DispatchRequest(ctx context.Context, request Request) *Response

func (*Dispatcher) RegisterMethod added in v0.67.0

func (a *Dispatcher) RegisterMethod(method string, handler Command)

func (*Dispatcher) RegisteredMethods added in v0.67.0

func (a *Dispatcher) RegisteredMethods() []string

type ErrorCode

type ErrorCode int16
const (
	// ErrorCodeParseError Invalid JSON was received by the server. An error
	// occurred on the server while parsing the JSON text.
	ErrorCodeParseError ErrorCode = -32700
	// ErrorCodeInvalidRequest The JSON sent is not a valid Request object.
	ErrorCodeInvalidRequest ErrorCode = -32600
	// ErrorCodeMethodNotFound The method does not exist / is not available.
	ErrorCodeMethodNotFound ErrorCode = -32601
	// ErrorCodeInvalidParams Invalid method parameter(s).
	ErrorCodeInvalidParams ErrorCode = -32602
	// ErrorCodeInternalError Internal JSON-RPC error.
	ErrorCodeInternalError ErrorCode = -32603

	// ErrorCodeServerError is a generic implementation-defined server error.
	// This can be used when error that does not cleanly map to the error codes
	// above occurs in the server.
	ErrorCodeServerError ErrorCode = -32000
)

type ErrorDetails

type ErrorDetails struct {
	// Code indicates the error type that occurred.
	Code ErrorCode `json:"code"`

	// Message provides a short description of the error.
	// The message SHOULD be limited to a concise single sentence.
	Message string `json:"message"`

	// Data is a primitive or a structured value that contains additional
	// information about the error. This may be omitted.
	// The value of this member is defined by the Server (e.g. detailed error
	// information, nested errors etc.).
	Data string `json:"data,omitempty"`
}

ErrorDetails is returned when an RPC call encounters an error.

func NewCustomError

func NewCustomError(code ErrorCode, message string, data error) *ErrorDetails

func NewInternalError

func NewInternalError(data error) *ErrorDetails

func NewInvalidParams

func NewInvalidParams(data error) *ErrorDetails

func NewInvalidRequest

func NewInvalidRequest(data error) *ErrorDetails

func NewMethodNotFound

func NewMethodNotFound(methodName string) *ErrorDetails

func NewParseError

func NewParseError(data error) *ErrorDetails

func NewServerError

func NewServerError(code ErrorCode, data error) *ErrorDetails

func NewUnsupportedMethod added in v0.67.0

func NewUnsupportedMethod(data error) *ErrorDetails

func (ErrorDetails) Error added in v0.63.0

func (d ErrorDetails) Error() string

func (ErrorDetails) IsInternalError

func (d ErrorDetails) IsInternalError() bool

type InterceptorFunc added in v0.67.0

type InterceptorFunc func(ctx context.Context, request Request) *ErrorDetails

type Params

type Params interface{}

Params is just a nicer way to describe what's passed to the handlers.

type Request

type Request struct {
	// Version specifies the version of the JSON-RPC protocol.
	// MUST be exactly "2.0".
	Version string `json:"jsonrpc"`

	// Method contains the name of the method to be invoked.
	Method string `json:"method"`

	// Params is a by-name Structured value that holds the parameter values to be
	// used during the invocation of the method. This member MAY be omitted.
	Params Params `json:"params,omitempty"`

	// ID is an identifier established by the Client that MUST contain a String.
	// If it is not included it is assumed to be a notification.
	// The Server MUST reply with the same value in the Response object if included.
	// This member is used to correlate the context between the two objects.
	ID string `json:"id,omitempty"`
}

func (*Request) Check

func (r *Request) Check() error

func (*Request) IsNotification

func (r *Request) IsNotification() bool

type Response

type Response struct {
	// Version specifies the version of the JSON-RPC protocol.
	// MUST be exactly "2.0".
	Version string `json:"jsonrpc"`

	// Result is REQUIRED on success. This member MUST NOT exist if there was an
	// error invoking the method.
	Result Result `json:"result,omitempty"`

	// Error is REQUIRED on error. This member MUST NOT exist if there was no
	// error triggered during invocation.
	Error *ErrorDetails `json:"error,omitempty"`

	// ID is an identifier established by the Client that MUST contain a String.
	// This member is REQUIRED. It MUST be the same as the value of the id member
	// in the Request Object.
	// If there was an error in detecting the id in the Request object (e.g.
	// Parse error/Invalid Request), it MUST be empty.
	ID string `json:"id,omitempty"`
}

func NewErrorResponse

func NewErrorResponse(id string, details *ErrorDetails) *Response

func NewSuccessfulResponse

func NewSuccessfulResponse(id string, result Result) *Response

type Result

type Result interface{}

Result is just a nicer way to describe what's expected to be returned by the handlers.

type TraceID added in v0.67.0

type TraceID string

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL