[katello-devel] Introducing ApiError in the ApiController

Lukas Zapletal lzap at redhat.com
Thu Jul 28 09:12:01 UTC 2011


Hello,

while I was tracing one issue in our API controllers I have found we are 
throwing away original exceptions. Example:

def import_manifest
   # ....
   # ....
   # ....
   rescue => e
     raise _("Manifest import failed")
end

The problem with the code above is we are loosing the original exception 
and I have to either insert some tracing statements or dig in the 
backend engine logs to find out what's wrong.

I have introduced new class ApiError and new handler in the 
ApiController that prints the message, original error message and 
original error backtrace. It also returns both messages (but not 
backtrace) to the client.

class ApiError < StandardError
   attr_reader :original

   def initialize(msg, original=$!)
     super(msg)
     @original = original
   end
end

rescue_from ApiError, :with => proc { |e| render_api_exception(500, e) }

The only change we need to do is to raise this type instead of the 
original type. It will accept the user-friendly message and the second 
parameter is optional (default = last exception thrown).

def import_manifest
   # ....
   # ....
   # ....
   rescue => e
     raise ApiError, _("Manifest import failed")
end

That's all. If there are no objections I will update all the places.

-- 
Later,

  Lukas Zapletal | E32E400A
  RHN Satellite Engineering
  Red Hat Czech s.r.o. Brno




More information about the katello-devel mailing list