Bravia.Error exception (Bravia v0.1.0)

Copy Markdown View Source

A normalized failure from a BRAVIA call.

Two very different things end up here. Sony returns application errors with an HTTP 200 status and an "error" array in the body; those carry a :code. Anything that fails below that — a timeout, a refused connection — has no code and arrives via transport/2.

Match on :reason rather than :code; the codes are Sony's and the reasons are stable across them.

The reason worth handling explicitly is :unauthorized. It means the TV rejected the request because no valid Pre-Shared Key was sent. Enable one on the TV under Settings → Network → Home Network → IP Control → Authentication by selecting "Normal and Pre-Shared Key", then pass it as :psk to Bravia.new/2.

Summary

Functions

Maps a Sony error code to a reason atom, or :unknown.

Builds an error from a Sony [code, message] pair.

Builds an error for a failure below the application layer, such as a timeout.

Types

reason()

@type reason() ::
  :illegal_argument
  | :illegal_request
  | :illegal_state
  | :no_such_method
  | :unsupported_version
  | :unsupported_operation
  | :display_off
  | :unauthorized
  | :unknown
  | atom()

t()

@type t() :: %Bravia.Error{
  __exception__: true,
  code: integer() | nil,
  message: String.t() | nil,
  method: String.t() | nil,
  reason: reason(),
  service: String.t() | nil
}

Functions

from_code(code)

@spec from_code(integer()) :: reason()

Maps a Sony error code to a reason atom, or :unknown.

iex> Bravia.Error.from_code(12)
:no_such_method

new(code, message, opts \\ [])

@spec new(integer(), String.t() | nil, keyword()) :: t()

Builds an error from a Sony [code, message] pair.

Sony's message is frequently just the method name rather than prose, so it is carried through untouched but should not be shown to users on its own.

transport(reason, opts \\ [])

@spec transport(
  atom(),
  keyword()
) :: t()

Builds an error for a failure below the application layer, such as a timeout.