Module: Browserctl::Commands::CliOutput

Included in:
Ask, Click, Cookie, Daemon, Dialog, Fill, Flow, Page, Resume, Screenshot, State, Storage, Workflow
Defined in:
lib/browserctl/commands/cli_output.rb

Constant Summary collapse

AUTH_REQUIRED_EXIT_CODE =
Browserctl::AuthRequiredError::AUTH_REQUIRED_EXIT_CODE

Instance Method Summary collapse

Instance Method Details

#exit_code_for(res) ⇒ Object

Maps a daemon error response onto a process exit code. Defaults to 1; special-cased only for codes that callers programmatically branch on.



39
40
41
42
43
# File 'lib/browserctl/commands/cli_output.rb', line 39

def exit_code_for(res)
  return AUTH_REQUIRED_EXIT_CODE if (res[:code] || res["code"]) == "AUTH_REQUIRED"

  1
end

Print the JSON-RPC daemon response, routed through the active ‘OutputFormat`. The historical default behaviour was `puts res.to_json` — keeping that as the `text` branch preserves byte-identical output for existing callers and golden files. `json` mode emits the same JSON explicitly. `silent` suppresses stdout entirely; errors still write the structured payload to stderr because errors are the result, not cosmetic output.

‘text_block` (optional) overrides the JSON dump in `text` mode for commands that have a distinct human-readable form (e.g. `init`).



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/browserctl/commands/cli_output.rb', line 23

def print_result(res, text_block = nil)
  fmt = OutputFormat.current

  if res.is_a?(Hash) && (res[:error] || res["error"])
    message = res[:error] || res["error"]
    warn "Error: #{message}"
    warn structured_error_line(res, message)
    puts res.to_json unless fmt.silent?
    exit exit_code_for(res)
  end

  fmt.emit(res, text_block)
end

#structured_error_line(res, message) ⇒ Object

Builds the single-line structured payload emitted to stderr after the human-readable line. Agents parse this JSON deterministically. Shape: { code, message, context, suggested_action }.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/browserctl/commands/cli_output.rb', line 48

def structured_error_line(res, message)
  code    = (res[:code] || res["code"] || Browserctl::Error::Codes::GENERIC).to_s
  context = res[:context] || res["context"] || {}
  action  = res[:suggested_action] || res["suggested_action"] ||
            Browserctl::Error::SuggestedActions.for(code)
  JSON.generate(
    code: code,
    message: message,
    context: context,
    suggested_action: action
  )
end