Custom error handling question

I'm trying to port something I've done in Perl to Ruby and am getting
myself confused, or perhaps drowned in details between the various files.

I'm trying to write a custom error handler class.
The documentation tells me that you can extend the Error class with a
custom one that does more.

So I started with

class CustomError < StandardError

def initialise(arg1, arg2, arg3, *args)
   .....
   .....
end

def handle( opt1, opt2, opt3)
   ....
   ....
end
end

I then had a wrapper that wrapped around the code that did the work.

  def dothefanango(context, &codeblock)
    oops = CustomError.new( context, stuff, more_stuff)
    begin
        yield context
    rescue CustomError ...
           oops.handle(......)
           ....
    rescue CustomError other than the one above
           .......
    rescue StandardError ....
           ....
    else
           ....
  end

In principle, all well and good. In practice the details are killing me.

The pickaxe book says that the rescue clause can have parameters, but
I'm also concerned with the 'raise' passing back information - more than
just a string.

I'm drowning in the details and don't know what to try and experiment
with. Any advice would be appreciated.

···

--
This is not a novel to be tossed aside lightly.
It should be thrown with great force. -- Dorothy Parker

Some enlightenment.

Firstly, the CustomError.new goes in the block of code that is yeilded
to, not in the wrapper.

Secondly, even though I've found examples in the ruby libraray and rails
of handlers beign extended and having methods of their own, I cna;'t
get it o work.

For example, in actionpack/lib/action_view/base.rb
there is

   begin
        render_template(template_extension, template_source,
                        template_file_name, local_assigns)
      rescue Exception => e
        if TemplateError === e
          e.sub_template_of(template_file_name)
          ....

but when I try

  rescue CustomError => e
            puts "Caught #{e.message.to_s}"
            e.handle("#{e.message.to_s}",
                     "#{e.setup2.to_s}", "#{e.string3.to_s}")

the handle method doesn't get invoked. I don't get an error message
about it either.

I have, of course

class CustomError < StandardError

  attr_accessor :setup1, :setup2, :setup3

  def initialize(setup1, setup2, setup3)
    @setup1 = setup1
    @setup2 = setup2
    @setup3 = setup3
    puts "Custom Error handler registered with #{@setup1.to_s},
                                               #{@setup2.to_s},
                                               #{setup3.to_s}"
  end

  def handle( handle1, handle2, handle3)
     puts "Custom error handler handled"
     puts "\tsetup1 = #{@setup1}"
     puts "\tsetup2 = #{@setup2}"
     puts "\tsetup3 = #{@setup3}"
     puts "\thandle1 = #{handle1}"
     puts "\thandle2 = #{handle2}"
     puts "\thandle3 = #{handle3}"
  end

end

Anton Aylward said the following on 06/12/07 01:58 PM:

···

I'm trying to port something I've done in Perl to Ruby and am getting
myself confused, or perhaps drowned in details between the various files.

I'm trying to write a custom error handler class.
The documentation tells me that you can extend the Error class with a
custom one that does more.

So I started with

class CustomError < StandardError

def initialise(arg1, arg2, arg3, *args)
   .....
   .....
end

def handle( opt1, opt2, opt3)
   ....
   ....
end
end

I then had a wrapper that wrapped around the code that did the work.

  def dothefanango(context, &codeblock)
    oops = CustomError.new( context, stuff, more_stuff)
    begin
        yield context
    rescue CustomError ...
           oops.handle(......)
           ....
    rescue CustomError other than the one above
           .......
    rescue StandardError ....
           ....
    else
           ....
  end

In principle, all well and good. In practice the details are killing me.

The pickaxe book says that the rescue clause can have parameters, but
I'm also concerned with the 'raise' passing back information - more than
just a string.

I'm drowning in the details and don't know what to try and experiment
with. Any advice would be appreciated.

--
Asking if computers can think is like asking if submarines can swim.

Some enlightenment.

Firstly, the CustomError.new goes in the block of code that is yeilded
to, not in the wrapper.

Secondly, even though I've found examples in the ruby libraray and rails
of handlers beign extended and having methods of their own, I cna;'t
get it o work.

For example, in actionpack/lib/action_view/base.rb
there is

   begin
        render_template(template_extension, template_source,
                        template_file_name, local_assigns)
      rescue Exception => e
        if TemplateError === e
          e.sub_template_of(template_file_name)
          ....

but when I try

  rescue CustomError => e
            puts "Caught #{e.message.to_s}"
            e.handle("#{e.message.to_s}",
                     "#{e.setup2.to_s}", "#{e.string3.to_s}")

Is the 'puts "Caught #{e.message.to_s}"' being called? Do you see output from it? If the answer is NO, then my guess is that the statement in your code which raises a CustomError is not being called.

the handle method doesn't get invoked. I don't get an error message
about it either.

I have, of course

class CustomError < StandardError

  attr_accessor :setup1, :setup2, :setup3

  def initialize(setup1, setup2, setup3)
    @setup1 = setup1
    @setup2 = setup2
    @setup3 = setup3
    puts "Custom Error handler registered with #{@setup1.to_s},
                                               #{@setup2.to_s},
                                               #{setup3.to_s}"
  end

  def handle( handle1, handle2, handle3)
     puts "Custom error handler handled"
     puts "\tsetup1 = #{@setup1}"
     puts "\tsetup2 = #{@setup2}"
     puts "\tsetup3 = #{@setup3}"
     puts "\thandle1 = #{handle1}"
     puts "\thandle2 = #{handle2}"
     puts "\thandle3 = #{handle3}"
  end

end

Regards, Morton

···

On Dec 6, 2007, at 6:04 PM, Anton Aylward wrote: