jsonrpc2

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 8 Imported by: 9 modules, 30 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

	// ErrLimitExceeded is returned when a message was refused due to user exceeding
	// a limit (e.g. number of requests per second).
	ErrLimitExceeded int64 = -32002

	// ErrConflict is returned when a request conflicts with an existing request.
	ErrConflict int64 = -32003
)

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 canonical JSON string of the request excluding the auth field.

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"`
	Method  string     `json:"method"`
	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)

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

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