Documentation ¶
Index ¶
- Constants
- Variables
- func TraceIDFromContext(ctx context.Context) string
- type Command
- type Dispatcher
- type ErrorCode
- type ErrorDetails
- func NewCustomError(code ErrorCode, message string, data error) *ErrorDetails
- func NewInternalError(data error) *ErrorDetails
- func NewInvalidParams(data error) *ErrorDetails
- func NewInvalidRequest(data error) *ErrorDetails
- func NewMethodNotFound(methodName string) *ErrorDetails
- func NewParseError(data error) *ErrorDetails
- func NewServerError(code ErrorCode, data error) *ErrorDetails
- func NewUnsupportedMethod(data error) *ErrorDetails
- type InterceptorFunc
- type Params
- type Request
- type Response
- type Result
- type TraceID
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 ¶
Types ¶
type Command ¶
type Command interface {
Handle(ctx context.Context, params Params) (Result, *ErrorDetails)
}
type Dispatcher ¶
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 ¶
func NewDispatcher(log *zap.Logger) *Dispatcher
func (*Dispatcher) AddInterceptor ¶
func (a *Dispatcher) AddInterceptor(interceptor InterceptorFunc)
func (*Dispatcher) DispatchRequest ¶
func (a *Dispatcher) DispatchRequest(ctx context.Context, request Request) *Response
func (*Dispatcher) RegisterMethod ¶
func (a *Dispatcher) RegisterMethod(method string, handler Command)
func (*Dispatcher) RegisteredMethods ¶
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 ¶
func NewUnsupportedMethod(data error) *ErrorDetails
func (ErrorDetails) Error ¶
func (d ErrorDetails) Error() string
func (ErrorDetails) IsInternalError ¶
func (d ErrorDetails) IsInternalError() bool
type InterceptorFunc ¶
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) IsNotification ¶
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 ¶
Click to show internal directories.
Click to hide internal directories.