librelist archives

« back to archive

Catching exceptions

Catching exceptions

From:
Michel Demazure
Date:
2012-03-01 @ 17:29
I have a quite large Green Shoes app. I want to rescue all exceptions and
stay in Shoes. My main file is like :

 

require auxiliary files (200 methods)

begin

require the file with my shoes app

rescue

.

end

 

and errors are not rescued.

 

I guess I have to put everything within Shoes, as

 

begin

Shoes.app do

require all I need

my shoes code

rescue

..

end

 

Is that the solution ?

More generally, what is the general philosophy of exception management in
Shoes ?

 

Michel Demazure

 <mailto:michel@demazure.com> michel@demazure.com

 

 

 

Michel Demazure

michel@demazure.com

 

Re: [shoes] Catching exceptions

From:
Tobias Pfeiffer
Date:
2012-03-02 @ 01:13
Hey,

sorry but I don't really understand the problem or what "stay in Shoes" 
means. Clarification would be needed and I think it would also be good 
to see the full source code, maybe you could upload it to a service like 
pastebin?

Tobi

On 01.03.2012 18:29, Michel Demazure wrote:
>
> I have a quite large Green Shoes app. I want to rescue all exceptions 
> and stay in Shoes. My main file is like :
>
> require auxiliary files (200 methods)
>
> begin
>
> require the file with my shoes app
>
> rescue
>
> …
>
> end
>
> and errors are not rescued.
>
> I guess I have to put everything within Shoes, as
>
> begin
>
> Shoes.app do
>
> require all I need
>
> my shoes code
>
> rescue
>
> ….
>
> end
>
> Is that the solution ?
>
> More generally, what is the general philosophy of exception management 
> in Shoes ?
>
> Michel Demazure
>
> michel@demazure.com <mailto:michel@demazure.com>
>
> Michel Demazure
>
> michel@demazure.com
>

Re: [shoes] Catching exceptions

From:
Michel Demazure
Date:
2012-03-02 @ 07:47
Tobi, let me try to explain a bit more.

 

I am building a GUI to interface a quite large ruby application (25 
files). I organized my code in three parts. The application code (let say 
“business code”), the Shoes app reduced to the description part (flows and
stacks) and a module containing all the shoes code (methods called by 
buttons, methods to fill the choose widgets, connection to the business 
part,…).

 

My main file first requires the business part (configuring, connecting to 
databases,…), then open the Shoes app, which requires its methods module.

 

What I mean by “staying in Shoes” is this. I want exceptions raised in the
business code to be rescued *in* the Shoes app so that the app just 
continues after outputting a warning to the user. How should I do that ?

 

I tried various solutions without finding a way. Apparently, Shoes does 
not like to see begin/rescue blocks in its code, or does not understand 
it. Moreover (I do not know if it has something to do with the problem), 
the error I wanted to trap was a nasty one (when instance-eval’ing ruby 
code inputted in Shoes by the user…).

 

Do I have to design things so that the Shoes app is the first started 
code, requiring everything ? I did not try, because it would means 
modifying my module structure.

 

My more general question is : when you use Shoes for interfacing to 
something else than a toy application, with lot of files, tower of 
modules, logging, error management, configuration files, and so on, what 
is the “good practice” for code organization ? I generally use the 
classical gem structure.

 

And I do not speak of integration testing…

 

Michel Demazure

michel@demazure.com

 

De : shoes@librelist.com [mailto:shoes@librelist.com] De la part de Tobias
Pfeiffer
Envoyé : vendredi 2 mars 2012 02:14
À : shoes@librelist.com
Objet : Re: [shoes] Catching exceptions

 

Hey,

sorry but I don't really understand the problem or what "stay in Shoes" 
means. Clarification would be needed and I think it would also be good to 
see the full source code, maybe you could upload it to a service like 
pastebin?

Tobi

On 01.03.2012 18:29, Michel Demazure wrote: 

I have a quite large Green Shoes app. I want to rescue all exceptions and 
stay in Shoes. My main file is like :

 

require auxiliary files (200 methods)

begin

require the file with my shoes app

rescue

…

end

 

and errors are not rescued.

 

I guess I have to put everything within Shoes, as

 

begin

Shoes.app do

require all I need

my shoes code

rescue

….

end

 

Is that the solution ?

More generally, what is the general philosophy of exception management in Shoes ?

 

Michel Demazure

 <mailto:michel@demazure.com> michel@demazure.com

 

 

 

Michel Demazure

michel@demazure.com

 

 

Re: [shoes] Catching exceptions

From:
Steve Klabnik
Date:
2012-03-02 @ 10:01
Do you have an example of Shoes not working with begin/rescue/end?

Not that I necessarily _endorse_ this approach, but in Hackety, _why
put in this code:


require 'thread'
class Thread
  alias initialize_orig initialize
  def initialize *args, &blk
    initialize_orig *args do
      begin
        blk.call
      rescue => ex
        error ex
      end
    end
  end
end

Re: [shoes] Catching exceptions

From:
Michel Demazure
Date:
2012-03-02 @ 10:43
Steve,

Thanks for the snippet.
I will be away for a week without the possibility to work on the project.
I'll look again when I come back.
Michel Demazure
michel@demazure.com


-----Message d'origine-----
De : shoes@librelist.com [mailto:shoes@librelist.com] De la part de Steve
Klabnik
Envoyé : vendredi 2 mars 2012 11:01
À : shoes@librelist.com
Objet : Re: [shoes] Catching exceptions

Do you have an example of Shoes not working with begin/rescue/end?

Not that I necessarily _endorse_ this approach, but in Hackety, _why put in
this code:


require 'thread'
class Thread
  alias initialize_orig initialize
  def initialize *args, &blk
    initialize_orig *args do
      begin
        blk.call
      rescue => ex
        error ex
      end
    end
  end
end

Re: [shoes] Catching exceptions

From:
ashbb
Date:
2012-03-02 @ 13:20
Hi Michel,

> I have a quite large Green Shoes app.
Wow, fantastic!

> I want to rescue all exceptions and stay in shoes.
Interesting! Thank you for sharing the problem.
But sorry, I think it's difficult to solve your problem without revising
Green Shoes.

So, try out $CATCH_EXCEPTIONS variable after adding the following patch and
run the below test snippet. It may be not completly catching all
exceptions, but in most cases it'll keep to stay in Shoes. ;-)

If this patch helps you, I'll update Green Shoes. Send me a pull request. :)

== a test snippet ==

require 'green_shoes'

$CATCH_EXCEPTIONS = true

Shoes.app do
  button('raise'){raise 'exception test: button'}
  every(5){timer(0.01){raise 'exception test: timer'}}
  button('check'){append{para 'still alive!'}}
end


===== a patch ======

D:\green_shoes\lib\shoes>diff main_old.rb main.rb
1a2
>   $CATCH_EXCEPTIONS = false
109c110,116
<     Gtk.main if @main_app == app
---
>     begin
>       Gtk.main if @main_app == app
>     rescue
>       puts $!
>       retry
>     end
>

D:\green_shoes\lib\shoes>diff ruby_old.rb ruby.rb
106,108c106,110
<   def exit
<     Gtk.main_quit
<     File.delete Shoes::TMP_PNG_FILE if File.exist? Shoes::TMP_PNG_FILE
---
>   def exit flag=nil
>     unless flag and $CATCH_EXCEPTIONS
>       Gtk.main_quit
>       File.delete Shoes::TMP_PNG_FILE if File.exist? Shoes::TMP_PNG_FILE
>     end

ashbb

Re: [shoes] Catching exceptions

From:
Michel Demazure
Date:
2012-03-12 @ 13:29
Hi,

 

Yes, it works ! At least in the sense that the Shoes app just ignores the 
error. That’s already much better.

 

Apparently, what is added in ‘main.rb’ after the ‘rescue’ does not do 
anything. The error report goes to the console, even without the ‘puts $!’
; raising again here does nothing.

 

Is there a way to know (inside the Shoes app) that an error occurred ?

 

Michel Demazure

michel@demazure.com

 

De : shoes@librelist.com [mailto:shoes@librelist.com] De la part de ashbb
Envoyé : vendredi 2 mars 2012 14:20
À : shoes@librelist.com
Objet : Re: [shoes] Catching exceptions

 

Hi Michel,

> I have a quite large Green Shoes app.
Wow, fantastic!

> I want to rescue all exceptions and stay in shoes.
Interesting! Thank you for sharing the problem.
But sorry, I think it's difficult to solve your problem without revising 
Green Shoes.

So, try out $CATCH_EXCEPTIONS variable after adding the following patch 
and run the below test snippet. It may be not completly catching all 
exceptions, but in most cases it'll keep to stay in Shoes. ;-)

If this patch helps you, I'll update Green Shoes. Send me a pull request. :)

== a test snippet ==

require 'green_shoes'

$CATCH_EXCEPTIONS = true

Shoes.app do
  button('raise'){raise 'exception test: button'}
  every(5){timer(0.01){raise 'exception test: timer'}}
  button('check'){append{para 'still alive!'}}
end


===== a patch ======

D:\green_shoes\lib\shoes>diff main_old.rb main.rb
1a2
>   $CATCH_EXCEPTIONS = false
109c110,116
<     Gtk.main if @main_app == app
---
>     begin
>       Gtk.main if @main_app == app
>     rescue
>       puts $!
>       retry
>     end
>

D:\green_shoes\lib\shoes>diff ruby_old.rb ruby.rb
106,108c106,110
<   def exit
<     Gtk.main_quit
<     File.delete Shoes::TMP_PNG_FILE if File.exist? Shoes::TMP_PNG_FILE
---
>   def exit flag=nil
>     unless flag and $CATCH_EXCEPTIONS
>       Gtk.main_quit
>       File.delete Shoes::TMP_PNG_FILE if File.exist? Shoes::TMP_PNG_FILE
>     end

ashbb

Re: [shoes] Catching exceptions

From:
ashbb
Date:
2012-03-13 @ 11:35
Hi Michel,

Thank you for the confirmation.

> Is there a way to know (inside the Shoes app) that an error occurred?
Okay, how about the following?

- at first, replace `puts $!` with `$stderr.puts $!` in main.rb
- then, try out the below snippet

  require 'green_shoes'
  require 'stringio'

  $CATCH_EXCEPTIONS = true

  errlog = './errmsg.log'
  $stderr.reopen errlog

  Shoes.app do
    button('raise'){raise 'exception test: button'}
    every(5){timer(0.01){raise 'exception test: timer'}}
    button('check'){append{para 'still alive!'}}
    button('err log'){Shoes.app{para IO.read(errlog).gsub('<', '<')}}
  end

Hope this helps,
ashbb

Re: [shoes] Catching exceptions

From:
Michel Demazure
Date:
2012-03-13 @ 14:42
Hi,

 

As I said, yes, with your snippets added, errors are “ignored” : the Shoes
app just goes on. But they are not “trapped”, the rescue statement added 
in main.rb is useless : nothing after the  rescue is ever hit, just try 
and put anything there. Moreover, $! is always written to the console 
(STDERR I guess), even without any statement to this effect in the rescue 
block. Something happens somewhere else in between.

 

Yours,

 

Michel

michel@demazure.com

 

De : shoes@librelist.com [mailto:shoes@librelist.com] De la part de ashbb
Envoyé : mardi 13 mars 2012 12:35
À : shoes@librelist.com
Objet : Re: [shoes] Catching exceptions

 

Hi Michel,

Thank you for the confirmation.

> Is there a way to know (inside the Shoes app) that an error occurred?
Okay, how about the following?

- at first, replace `puts $!` with `$stderr.puts $!` in main.rb
- then, try out the below snippet

  require 'green_shoes'
  require 'stringio'
  
  $CATCH_EXCEPTIONS = true
  
  errlog = './errmsg.log'
  $stderr.reopen errlog
  
  Shoes.app do
    button('raise'){raise 'exception test: button'}
    every(5){timer(0.01){raise 'exception test: timer'}}
    button('check'){append{para 'still alive!'}}
    button('err log'){Shoes.app{para IO.read(errlog).gsub('<', '<')}}
  end

Hope this helps,
ashbb

Re: [shoes] Catching exceptions

From:
ashbb
Date:
2012-03-13 @ 21:27
Hi Michel,

Umm,... did you try the new test snippet?
I added the following three lines in the test snippet.

  errlog = './errmsg.log'
  $stderr.reopen errlog
  button('err log'){Shoes.app{para IO.read(errlog).gsub('<', '<')}}

> Moreover, $! is always written to the console (STDERR I guess)
Yes, you are right.
But if you add the above three lines, I think error messages will be
written in the file './errmsg.log'.

ashbb

Re: [shoes] Catching exceptions

From:
Michel Demazure
Date:
2012-03-02 @ 16:10
Many thanks, I’ll try that and report to you.

 

Michel Demazure

michel@demazure.com

 

De : shoes@librelist.com [mailto:shoes@librelist.com] De la part de ashbb
Envoyé : vendredi 2 mars 2012 14:20
À : shoes@librelist.com
Objet : Re: [shoes] Catching exceptions

 

Hi Michel,

> I have a quite large Green Shoes app.
Wow, fantastic!

> I want to rescue all exceptions and stay in shoes.
Interesting! Thank you for sharing the problem.
But sorry, I think it's difficult to solve your problem without revising 
Green Shoes.

So, try out $CATCH_EXCEPTIONS variable after adding the following patch 
and run the below test snippet. It may be not completly catching all 
exceptions, but in most cases it'll keep to stay in Shoes. ;-)

If this patch helps you, I'll update Green Shoes. Send me a pull request. :)

== a test snippet ==

require 'green_shoes'

$CATCH_EXCEPTIONS = true

Shoes.app do
  button('raise'){raise 'exception test: button'}
  every(5){timer(0.01){raise 'exception test: timer'}}
  button('check'){append{para 'still alive!'}}
end


===== a patch ======

D:\green_shoes\lib\shoes>diff main_old.rb main.rb
1a2
>   $CATCH_EXCEPTIONS = false
109c110,116
<     Gtk.main if @main_app == app
---
>     begin
>       Gtk.main if @main_app == app
>     rescue
>       puts $!
>       retry
>     end
>

D:\green_shoes\lib\shoes>diff ruby_old.rb ruby.rb
106,108c106,110
<   def exit
<     Gtk.main_quit
<     File.delete Shoes::TMP_PNG_FILE if File.exist? Shoes::TMP_PNG_FILE
---
>   def exit flag=nil
>     unless flag and $CATCH_EXCEPTIONS
>       Gtk.main_quit
>       File.delete Shoes::TMP_PNG_FILE if File.exist? Shoes::TMP_PNG_FILE
>     end

ashbb