File: exit_error.rb

package info (click to toggle)
ruby-tty-command 0.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 452 kB
  • sloc: ruby: 1,990; makefile: 4; sh: 4
file content (31 lines) | stat: -rw-r--r-- 819 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

module TTY
  class Command
    # An ExitError reports an unsuccessful exit by command.
    #
    # The error message includes:
    #  * the name of command executed
    #  * the exit status
    #  * stdout bytes
    #  * stderr bytes
    #
    # @api private
    class ExitError < RuntimeError
      def initialize(cmd_name, result)
        super(info(cmd_name, result))
      end

      def info(cmd_name, result)
        "Running `#{cmd_name}` failed with\n" \
        "  exit status: #{result.exit_status}\n" \
        "  stdout: #{extract_output(result.out)}\n" \
        "  stderr: #{extract_output(result.err)}\n"
      end

      def extract_output(value)
        (value || "").strip.empty? ? "Nothing written" : value.strip
      end
    end # ExitError
  end # Command
end # TTY