Open operations

I suppose a good developer would wrap all his code in begin rescue. I don’t, and the result has always been errors show up in the console and the method stops execution at the point where the error occurred.

I did notice recently though that if an error occurs in an operation the operation is left open. The result is that my UI::HtmlDialog no longer responds to dialog.execute_script(script).

As a work around I created my own wrapper that automatically aborts the operation when an error occurs. It seems to work fine, but I thought I would ask for opinions before I do a mass replacement.

  def do_operation(op_name, disable_ui = false, next_transparent = false, transparent = false, &block)
    Sketchup.active_model.start_operation(op_name, disable_ui, next_transparent, transparent)
    begin
      block.call if block_given?
    rescue StandardError => e
      Sketchup.active_model.abort_operation
      raise e
    end
    Sketchup.active_model.commit_operation
  end

def do_something
  do_operation('Error Prone Task', true) do
    #do error prone tasks here.
  end
end

def do_something_else(operation = false)
  pr = proc do
    #error prone code here
  end
  if operation
    do_operation('Task', true) { pr.call }
  else
    pr.call
  end
end

This is another edition …

  def do_operation(op_name, disable_ui = false,
    next_transparent = false, transparent = false
  )
    return nil unless block_given?
    model = Sketchup.active_model
    begin
      model.start_operation(op_name, disable_ui, next_transparent, transparent)
      result = yield
    rescue => e
      model.abort_operation
      raise e
    else
      model.commit_operation
      return result
    end
  end

We’ve discussed some of this before …

The 2016 release Observer Updates primer mentions this:

… and we have a couple of tracker issue threads open:

1 Like