module GnuplotRB::ErrorHandling

Mixin for classes that need to run subprocess and handle errors from its stderr.

Public Instance Methods

check_errors(raw: false) click to toggle source

Check if there were errors in previous commands. Throws GnuplotError in case of any errors.

# File lib/gnuplotrb/mixins/error_handling.rb, line 14
def check_errors(raw: false)
  return if @err_array.empty?
  command = ''
  rest = ''
  @semaphore.synchronize do
    command = @err_array.first
    rest = @err_array[1..-1].join('; ')
    @err_array.clear
  end
  message = if raw
    "#{command};#{rest}}"
  else
    "Error in previous command (\"#{command}\"): \"#{rest}\""
  end
  fail GnuplotError, message
end

Private Instance Methods

handle_stderr(stream) click to toggle source

Start new thread that will read stderr given as stream and add errors into @err_array.

# File lib/gnuplotrb/mixins/error_handling.rb, line 36
def handle_stderr(stream)
  @err_array = []
  # synchronize access to @err_array
  @semaphore = Mutex.new
  Thread.new do
    until (line = stream.gets).nil?
      line.strip!
      @semaphore.synchronize { @err_array << line if line.size > 3 }
    end
  end
end