Snodo.Error (snodo v0.1.0)

Copy Markdown View Source

A protocol-neutral error with JSON-RPC-compatible defaults.

Summary

Functions

Builds an application authorization refusal.

Wraps a failure reason as an execution error.

Builds an internal error: code -32603, kind :execution.

Builds an invalid params error: code -32602, kind :protocol.

Builds a JSON-RPC invalid request error: code -32600, kind :json_rpc.

Builds a method-not-found error for method: code -32601, kind :protocol, message "Method not found: <method>".

Builds a JSON-RPC parse error: code -32700, kind :json_rpc.

Returns the JSON-RPC error object: "code", "message", and "data" when data is not nil. kind and cause are not included.

Types

kind()

@type kind() ::
  :json_rpc | :protocol | :transport | :execution | :authorization | :extension

t()

@type t() :: %Snodo.Error{
  cause: term() | nil,
  code: integer(),
  data: term() | nil,
  kind: kind(),
  message: String.t()
}

Functions

authorization(code, message, data \\ nil)

@spec authorization(integer(), String.t(), term() | nil) :: t()

Builds an application authorization refusal.

code is the application's own choice; JSON-RPC reserves -32000..-32099 for implementation-defined server errors. snodo never invents one.

execution(error)

@spec execution(term()) :: t()

Wraps a failure reason as an execution error.

An Snodo.Error is returned unchanged. Any other reason becomes code -32603 with the message "Tool execution failed", kind :execution, and reason in the cause field, which to_json_rpc/1 leaves out.

internal(message \\ "Internal error", cause \\ nil)

@spec internal(String.t(), term() | nil) :: t()

Builds an internal error: code -32603, kind :execution.

cause is kept in the struct's cause field. to_json_rpc/1 leaves it out, so it is not sent to the client.

invalid_params(message \\ "Invalid params", data \\ nil)

@spec invalid_params(String.t(), term() | nil) :: t()

Builds an invalid params error: code -32602, kind :protocol.

data, when not nil, is sent as the error's "data" and must be a JSON value. Return this from a handler when the request should fail as a JSON-RPC error, for example a resource that does not exist.

invalid_request(message \\ "Invalid Request")

@spec invalid_request(String.t()) :: t()

Builds a JSON-RPC invalid request error: code -32600, kind :json_rpc.

method_not_found(method)

@spec method_not_found(String.t()) :: t()

Builds a method-not-found error for method: code -32601, kind :protocol, message "Method not found: <method>".

parse_error(message \\ "Parse error")

@spec parse_error(String.t()) :: t()

Builds a JSON-RPC parse error: code -32700, kind :json_rpc.

to_json_rpc(error)

@spec to_json_rpc(t()) :: map()

Returns the JSON-RPC error object: "code", "message", and "data" when data is not nil. kind and cause are not included.