jsonrpc2

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2025 License: MIT Imports: 6 Imported by: 25

Documentation

Index

Constants

View Source
const (
	JsonRpcVersion = "2.0"

	// ErrUnknown should be used for all non-coded errors.
	ErrUnknown int64 = -32001

	// ErrParse is used when invalid JSON was received by the server.
	ErrParse int64 = -32700

	//ErrInvalidRequest is used when the JSON sent is not a valid Request object.
	ErrInvalidRequest int64 = -32600

	// ErrMethodNotFound should be returned by the handler when the method does
	// not exist / is not available.
	ErrMethodNotFound int64 = -32601

	// ErrInvalidParams should be returned by the handler when method
	// parameter(s) were invalid.
	ErrInvalidParams int64 = -32602

	// ErrInternal is not currently returned but defined for completeness.
	ErrInternal int64 = -32603

	//ErrServerOverloaded is returned when a message was refused due to a
	//server being temporarily unable to accept any new messages.
	ErrServerOverloaded int64 = -32000
)

Variables

This section is empty.

Functions

func EncodeErrorReponse

func EncodeErrorReponse[ErrorData any](id string, err *WireError) ([]byte, error)

func EncodeRequest

func EncodeRequest[Params any](request *Request[Params]) ([]byte, error)

func EncodeResponse

func EncodeResponse[Result any](response *Response[Result]) ([]byte, error)

Types

type Request

type Request[Params any] struct {
	Version string  `json:"jsonrpc"`
	ID      string  `json:"id"`
	Method  string  `json:"method"`
	Params  *Params `json:"params"`
	// Auth is used to store the JWT token for the request. It is not part of the JSON RPC specification.
	// JWT token can be part of the request payload or attached to the request header.
	Auth string `json:"auth,omitempty"`
}

Wrapping/unwrapping Message objects into JSON RPC ones folllowing https://www.jsonrpc.org/specification

func DecodeRequest

func DecodeRequest[Params any](requestBytes []byte, jwtTokenFromHeader string) (Request[Params], error)

func (*Request[Params]) Digest

func (r *Request[Params]) Digest() (string, error)

Digest returns a digest of the request. This is used for signature verification. The digest is a SHA256 hash of the JSON string of the request.

func (*Request[Params]) ServiceName

func (r *Request[Params]) ServiceName() string

type Response

type Response[Result any] struct {
	Version string     `json:"jsonrpc"`
	ID      string     `json:"id"`
	Result  *Result    `json:"result,omitempty"`
	Error   *WireError `json:"error,omitempty"`
}

func DecodeResponse

func DecodeResponse[Result any](responseBytes []byte) (Response[Result], error)

func (*Response[Result]) Digest added in v0.8.0

func (r *Response[Result]) Digest() (string, error)

type WireError

type WireError struct {
	// Code is an error code indicating the type of failure.
	Code int64 `json:"code"`
	// The Message is a short description of the error.
	Message string `json:"message"`
	// Data is optional structured data containing additional information about the error.
	Data *json.RawMessage `json:"data,omitempty"`
}

WireError represents a structured error in a Response.

func (*WireError) Error

func (w *WireError) Error() string

Jump to

Keyboard shortcuts

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