JSONRPC2 v2.0.0 JSONRPC2.Server.Handler behaviour View Source

A transport-agnostic server handler for JSON-RPC 2.0.

Example

defmodule SpecHandler do
  use JSONRPC2.Server.Handler

  def handle_request("subtract", [x, y]) do
    x - y
  end

  def handle_request("subtract", %{"minuend" => x, "subtrahend" => y}) do
    x - y
  end

  def handle_request("update", _) do
    :ok
  end

  def handle_request("sum", numbers) do
    Enum.sum(numbers)
  end

  def handle_request("get_data", []) do
    ["hello", 5]
  end
end

SpecHandler.handle(~s({"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}))
#=> ~s({"jsonrpc": "2.0", "result": 19, "id": 1})

Link to this section Summary

Callbacks

Respond to a request for method with params.

Link to this section Callbacks

Link to this callback

handle_request(method, params)

View Source

Specs

handle_request(method :: JSONRPC2.method(), params :: JSONRPC2.params()) ::
  JSONRPC2.json() | no_return()

Respond to a request for method with params.

You can return any serializable result (which will be ignored for notifications), or you can throw these values to produce error responses:

  • :method_not_found, :invalid_params, :internal_error, :server_error
  • any of the above, in a tuple like {:method_not_found, %{my_error_data: 1}} to return extra data
  • {:jsonrpc2, code, message} or {:jsonrpc2, code, message, data} to return a custom error, with or without extra data.