[Nitro] How do I gracefully handle db error conditions either from .rb or .xhtml files?

Jonathan Buch john at oxyliquit.de
Fri Aug 18 15:53:56 EDT 2006


Hi,

> OK, so a duplicate key constraint violation throws an error.  How do I
> capture this( or any db error condition for that matter ) so that I can
> gracefully inform the user of the violation and return them to the data
> entry page w/o having the app dump the full debug content to the screen

Using validations in Og models lets you check specifically for such  
errors.  But they don't protect you from bad SQL, it just protects you  
 from errors which you can predict that will happen.

For example, a unique constraint like your error message has shown:

class OgExampleModel
   property :message_id, String

   validate_unique :message_id
end

Now in a controller you can check if an object is valid:

class ExampleController < Nitro::Controller
   def add_obj
     obj = OgExampleModel.new
     obj.message_id = 5
     if obj.valid?
       obj.save
     else
       flash[:err] = 'Zonk! Input shite!'
     end
   end
end

Now in your .xhtml

<html>
<!-- other stuff --!>
<p color='red'>
   #{flash[:err]}
</p>
</html>

Like I said, this will only protect you from known error sources, all  
others will still get you the error screen, but it's a huge advantage.

Hope that helps.

Jo

-- 
Feel the love
http://pinkjuice.com/pics/ruby.png


More information about the Nitro-general mailing list