jsonrpc

package
v0.0.0-...-5bd881e Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package jsonrpc provides primitives to serve JSON-RPC requests.

Index

Constants

View Source
const NotificationCancelRequest = "$/cancelRequest"

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Code    ErrorCode `json:"code"`
	Message string    `json:"message"`
	Data    any       `json:"data,omitempty"`
}

func NewError

func NewError(code ErrorCode, err error) *Error

func WrapError

func WrapError(err error) *Error

WrapError wraps error as json-rpc error.

If error is jsonrpc.Error - returns original value.

func (*Error) AsResponse

func (err *Error) AsResponse(reqID int) *Response

func (*Error) Error

func (err *Error) Error() string

type ErrorCode

type ErrorCode int
const (
	ErrorCodeParseError     ErrorCode = -32700
	ErrorCodeInvalidRequest ErrorCode = -32600
	ErrorCodeMethodNotFound ErrorCode = -32601
	ErrorCodeInvalidParams  ErrorCode = -32602
	ErrorCodeInternalError  ErrorCode = -32603
)

func (ErrorCode) Error

func (code ErrorCode) Error(err error) *Error

func (ErrorCode) Errorf

func (code ErrorCode) Errorf(format string, args ...any) *Error

type Interceptor

type Interceptor interface {
	InterceptRequest(ctx context.Context, req Request, next Interceptor) *Response
}

type Request

type Request struct {
	ID     int             `json:"id,omitempty"`
	Method string          `json:"method"`
	Params json.RawMessage `json:"params,omitempty"`
}

type RequestHandler

type RequestHandler interface {
	HandleRequest(ctx context.Context, params json.RawMessage) (any, error)
}

func NewHandler

func NewHandler[TRequest, TResponse any](fn func(ctx context.Context, req TRequest) (*TResponse, error)) RequestHandler

NewHandler constructs a new type-safe request handler.

Incoming request is automatically parsed into a [TRequest] type.

type Response

type Response struct {
	ID     int    `json:"id"`
	Result any    `json:"result,omitempty"`
	Error  *Error `json:"error,omitempty"`
}

type ServeMux

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

func NewServeMux

func NewServeMux(handlers map[string]RequestHandler) *ServeMux

func (*ServeMux) ServeStream

func (mux *ServeMux) ServeStream(ctx context.Context, conn net.Conn) error

ServeStream handles incoming requests from a given connection.

func (*ServeMux) SetInterceptor

func (mux *ServeMux) SetInterceptor(interceptor Interceptor)

SetInterceptor adds request interceptors.

Jump to

Keyboard shortcuts

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