Sha256: 301974795aeed2ae0051e94dd44781d9437294d9cc4bb8ca26c6014d8c70baf8

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

# encoding: UTF-8

module Rosette
  module Core

    # Prints errors.
    #
    # @!attribute [r] stream
    #   @return [#write] a stream-like object to print errors to.
    # @!attribute [r] print_stack_trace
    #   @return [Boolean] whether or not to print a stack trace along with
    #     the error message.
    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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rosette-core-1.0.1 lib/rosette/core/error_reporters/printing_error_reporter.rb