Forum: Ruby on Rails hackish error handling

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bamat Est (Guest)
on 2006-02-13 17:32
(Received via mailing list)
I've got a block of code that's turning out.. well - just plain ugly.
So i know I must not be doing it the Ruby way.

The main problem I'm having is in dealing with handling errors - and
just not finding much Rails documentation that gives a deeper
understanding of how to handle errors in Rails (outside of several
docs on the catch-all's)

the main method i'm having problem with is a method in my controller
that recieves shipping information about a customer from my order
system and is supposed to return a shipping label back to the user.

The problem is that i want to validate that the form is sending me all
the required fieds using ActiveRecords validation rules, then I need
to submit the address in the order via XML to an external address
verification service, use that data to make any changes to the order
and then submit the order to the shipper via XML and recieve back
their response - interpret it and then display the shipping label to
the end user if everything went through succesfully.

So i could fail on ActiveRecord Validation - or either of those two
XML request can send back an error code. In all three cases I want to
send the user back to the form they came from and display either the
active record errors or the XML error messages.

You can see my two "hackish" approaches to catching the errors with
the XML errors with an if else in the verfication and a rescue at the
bottom to catch times when the success values i expect are not
present. But trying to add in the ActiveRecord validation at the top
( causes the error to fall straight down to the rescue clause
even when i try to intercept it with if else clauses, etc.  Using
redirect_to :back was another hack i don't like. It sends the user
back to whichever page they came from (5 potential input forms) - but
it loses all the form data that they submitted into the order.

I pretty much hate the way this is working today, but was under a
timeline crunch to get it done by today so I swallowed my pride and
hacked it up. But I've got an extra day now and would really love it
if someone could point me in the right direction to fixing these
mistakes. Everything i'm trying only seems to make the code uglier and

  def label
     require 'fileutils'
     require  'xmlsimple'

    @submitted = params[:FedExOrder]

    go =[:FedExOrder])

    @fedex =
    :name => @submitted['name'],
    :company =>  @submitted['company'],
    :phone => @submitted['phone'],
    :email  => @submitted['email'],
    :address => @submitted['address'],
    :address2 => @submitted['address2'],
    :city =>  @submitted['city'],
    :state => @submitted['state'],
    :zip  => @submitted['zip'])

    verification = XmlSimple::xml_in(@fedex.verify_address)

     if  verification['Match']
        if verification['Match'][0]['Score'].to_s  == '100'
                  @fedex.address =
                  @fedex.address2 =
         =  verification['Match'][0]['City'].to_s
                  @fedex.state =
                 flash[:notice] =
                 redirect_to :back
         flash[:notice] = verification['Error'][0]['Message']
         redirect_to :back

    mylabel = @fedex.label
    @label =  "#{mylabel.tracking_number}.png"
    FileUtils.copy_file  mylabel.image.path, "/localpath//#{@label}"

               flash[:notice] = mylabel.message
               redirect_to :back
Bamat Est (Guest)
on 2006-02-14 23:29
(Received via mailing list)
No suggestions? Did I make the question too complicated?
This topic is locked and can not be replied to.