[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


> 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

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?
       flash[:err] = 'Zonk! Input shite!'

Now in your .xhtml

<!-- other stuff --!>
<p color='red'>

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.


Feel the love

More information about the Nitro-general mailing list