rescue

How to rescue an eval in Ruby?

I'm trying to figure out how to rescue syntax errors that come up when eval()ing code in Ruby 1.8.6. I would expect the following Ruby code: #!/usr/bin/ruby good_str = "(1+1)" bad_str = "(1+1" # syntax error: missing closing paren begin puts eval(good_str) puts eval(bad_str) rescue => exc puts "RESCUED!" end to produ...

For ASP.NET MVC Rescues View, how do we add ViewData fields?

Hi, I have several Rescues defined for possible faults, however I am unable to access the ViewData to populate it with things we need the masterpage requires to render. Is this hidden away in a controller we can utilise? Thanks, ...

When creating an object in Ruby on Rails, which method of saving do you prefer, and why?

When writing the "create" method for an object in a Ruby on Rails app, I have used two methods. I would like to use one method for the sake of cleaner and more consistent code. I will list the two methods below. Does anyone know if one is better than the other? If so, why? Method 1: def create1 # is this unsecure? should we grab ...

How do I disable rescue handlers in Ruby on Rails apps when I'm running functional tests?

I have a number of controllers in my Ruby on Rails apps with a rescue handler at the end of the action that basically catches any unhandled errors and returns some kind of "user friendly" error. However, when I'm doing rake test I'd like to have those default rescue handlers disabled so I can see the full error & stack trace. Is there an...

How to deal with not knowing what exceptions can be raised by a library method in Ruby?

This is somewhat of a broad question, but it is one that I continue to come across when programming in Ruby. I am from a largely C and Java background, where when I use a library function or method, I look at the documentation and see what it returns on error (usually in C) or which exceptions it can throw (in Java). In Ruby, the situa...

work with rescue in Rails

Hi, I am working with the following piece; def index @user = User.find(params[:id]) rescue flash[:notice] = "ERROR" redirect_to(:action => 'index') else flash[:notice] = "OK" redirect_to(:action => 'index') end Now I either case whether I have a correct ID or not, I am always getting "OK" in my view, what am I...

rescue Nokogiri error

I've a simple script that looks at Twitter username and gets me the location. But some of the username doesn't exist and I get error: /usr/lib/ruby/1.8/open-uri.rb:277:in `open_http': 404 Not Found (OpenURI::HTTPError) I've tried to rescue it, but I can't to make it work. Can anyone help? Thanks a = [] my_file = File.new("location.t...

Begin Rescue not catching error

I'm using some ruby code wrapped in a begin - rescue block but somehow it manages to still crash. the block of code looks like this: # Retrieve messages from server def get_messages @connection.select('INBOX') @connection.uid_search(['ALL']).each do |uid| msg = @connection.uid_fetch(uid,'RFC822').first.attr['RFC822'] begin...

Ruby c extensions: How can I catch all exceptions, including things that aren't StandardErrors?

In ruby, begin # ... rescue # ... end won't catch exceptions that aren't subclasses of StandardError. In C, rb_rescue(x, Qnil, y, Qnil); VALUE x(void) { /* ... */ return Qnil; } VALUE y(void) { /* ... */ return Qnil; } will do the same thing. How can I rescue Exception => e from a ruby C extension (instead of just rescue => e)...

Rescue and redirect 500 Error in Rails when Database is not found

We had some patches for our Database applied the other day. I was not given a heads up, and the application went down. We are getting nothing but a white Status: 500 Internal Server Error Content-Type: text/html 500 Internal Server Error page. We are using Rails 2.2.2 I want to redirect the user to /500.html so that they get th...