Class: Rosette::Core::PrintingErrorReporter

Inherits:
ErrorReporter show all
Defined in:
lib/rosette/core/error_reporters/printing_error_reporter.rb

Overview

Prints errors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ErrorReporter

#with_error_reporting

Constructor Details

#initialize(stream, options = {}) ⇒ PrintingErrorReporter

Creates a new error reporter.

Parameters:

  • stream (#write)

    The stream-like object to print errors to.

  • options (Hash) (defaults to: {})

    A hash of options. Can contain:

    • print_stack_trace: A boolean value indicating whether or not a stack trace should be printed alongside the error message.



23
24
25
26
# File 'lib/rosette/core/error_reporters/printing_error_reporter.rb', line 23

def initialize(stream, options = {})
  @stream = stream
  @print_stack_trace = options.fetch(:print_stack_trace, false)
end

Instance Attribute Details

Returns whether or not to print a stack trace along with the error message.

Returns:

  • (Boolean)

    whether or not to print a stack trace along with the error message.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rosette/core/error_reporters/printing_error_reporter.rb', line 13

class PrintingErrorReporter < ErrorReporter
  attr_reader :stream, :print_stack_trace
  alias :print_stack_trace? :print_stack_trace

  # Creates a new error reporter.
  #
  # @param [#write] stream The stream-like object to print errors to.
  # @param [Hash] options A hash of options. Can contain:
  #   * +print_stack_trace+: A boolean value indicating whether or not
  #     a stack trace should be printed alongside the error message.
  def initialize(stream, options = {})
    @stream = stream
    @print_stack_trace = options.fetch(:print_stack_trace, false)
  end

  # Print an error.
  #
  # @param [Exception] error The error to print.
  # @param [Hash] options A hash of associated options (will also be
  #   printed)
  # @return [void]
  def report_error(error, options = {})
    stream.write("#{error.message}\n")

    if print_stack_trace?
      Array(error.backtrace).each do |line|
        stream.write("#{line}\n")
      end

      stream.write(options.inspect)
    end
  end

  # Print a warning. Warnings are treated the same as errors.
  #
  # @param [Exception] error The error to print.
  # @param [Hash] options A hash of associated options (will also be
  #   printed)
  # @return [void]
  def report_warning(error, options = {})
    report_error(error, options)
  end
end

#stream#write (readonly)

Returns a stream-like object to print errors to.

Returns:

  • (#write)

    a stream-like object to print errors to.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rosette/core/error_reporters/printing_error_reporter.rb', line 13

class PrintingErrorReporter < ErrorReporter
  attr_reader :stream, :print_stack_trace
  alias :print_stack_trace? :print_stack_trace

  # Creates a new error reporter.
  #
  # @param [#write] stream The stream-like object to print errors to.
  # @param [Hash] options A hash of options. Can contain:
  #   * +print_stack_trace+: A boolean value indicating whether or not
  #     a stack trace should be printed alongside the error message.
  def initialize(stream, options = {})
    @stream = stream
    @print_stack_trace = options.fetch(:print_stack_trace, false)
  end

  # Print an error.
  #
  # @param [Exception] error The error to print.
  # @param [Hash] options A hash of associated options (will also be
  #   printed)
  # @return [void]
  def report_error(error, options = {})
    stream.write("#{error.message}\n")

    if print_stack_trace?
      Array(error.backtrace).each do |line|
        stream.write("#{line}\n")
      end

      stream.write(options.inspect)
    end
  end

  # Print a warning. Warnings are treated the same as errors.
  #
  # @param [Exception] error The error to print.
  # @param [Hash] options A hash of associated options (will also be
  #   printed)
  # @return [void]
  def report_warning(error, options = {})
    report_error(error, options)
  end
end

Instance Method Details

#report_error(error, options = {}) ⇒ void

This method returns an undefined value.

Print an error.

Parameters:

  • error (Exception)

    The error to print.

  • options (Hash) (defaults to: {})

    A hash of associated options (will also be printed)



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rosette/core/error_reporters/printing_error_reporter.rb', line 34

def report_error(error, options = {})
  stream.write("#{error.message}\n")

  if print_stack_trace?
    Array(error.backtrace).each do |line|
      stream.write("#{line}\n")
    end

    stream.write(options.inspect)
  end
end

#report_warning(error, options = {}) ⇒ void

This method returns an undefined value.

Print a warning. Warnings are treated the same as errors.

Parameters:

  • error (Exception)

    The error to print.

  • options (Hash) (defaults to: {})

    A hash of associated options (will also be printed)



52
53
54
# File 'lib/rosette/core/error_reporters/printing_error_reporter.rb', line 52

def report_warning(error, options = {})
  report_error(error, options)
end