breaking into the unknown…

Parsing exception in ruby

1 Comment

I have explained exception handling in this post and raising and handling custom exception in this post. Again I want to emphasize here is that ruby exception is just like any other class and if you have defined your own custom exception class, you can define attribute_accessor , you can define different methods in it and do all the stuffs you do with any other class. Now, if you have instance of your exception class, you can access all its method on it. By convention, I have denoted the instance or the exception object as e, in blog post I have mentioned above.

O.K.. So our exception has thrown exception and we have captured it as below

***** your code which you think may raise any exception****
rescue  Exception => e
****** write your handling code ******

So, we have collected the Exception object in e . Now, we need to parse the exception, say to display the error message or the get the content of response body if it is coming from some API call, or to just to divulge deeper into cause of exception. Well, we have a no of methods available to us. To get list, of all the methods just print e.methods. You can see below list of the methods


You can parse the exception by using the above methods to suit your needs. Some, of the common methods, You would have been using , if you have written even basic Exception handling code are as below

e.message # give you the exception message

e.backtrace # print the line through which the exception occurred

e.response # print the response

e.http_code # give the error code

e.http_body # give the content of body

Once, you have the response, you can then, modify it to display in the way you want. For example : Say, Iam calling a third party service, which return me below content as error message

“<SOAP-ENV:Envelope xmlns:SOAP-ENV=\”\”><SOAP-ENV:Header/><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Client</faultcode><faultstring xml:lang=\”en\”>Taxpayer ID(SSN) should be a 9 digit number.Date of Birth is required.Taxpayer ID(SSN) is required.</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>”

So, I got back xml wrapped in SOAP envelop in the response. You can see that my actual message is within <faultstring> </faultstring> node. So I will parse it as below


# some code which make call to third party service, which register a user for me

rescue Exception => e
# handling the exception object e
response = e.methods.include?(:response) ? (Nokogiri::XML e.response) : nil
fault_string = response.present? ? response.xpath("//faultstring").text : nil[:error] = fault_string.present? ? fault_string :  "You Account Failed to Create"
render :new


Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

One thought on “Parsing exception in ruby

  1. Great article. I am dealing with a few of these issues as well..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s