Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 39985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:57:06+00:00 2026-05-10T14:57:06+00:00

I’m new to Ruby, so I’m having some trouble understanding this weird exception problem

  • 0

I’m new to Ruby, so I’m having some trouble understanding this weird exception problem I’m having. I’m using the ruby-aaws gem to access Amazon ECS: http://www.caliban.org/ruby/ruby-aws/. This defines a class Amazon::AWS:Error:

module Amazon   module AWS     # All dynamically generated exceptions occur within this namespace.     #     module Error       # An exception generator class.       #       class AWSError         attr_reader :exception          def initialize(xml)           err_class = xml.elements['Code'].text.sub( /^AWS.*\./, '' )           err_msg = xml.elements['Message'].text            unless Amazon::AWS::Error.const_defined?( err_class )             Amazon::AWS::Error.const_set( err_class,                     Class.new( StandardError ) )           end            ex_class = Amazon::AWS::Error.const_get( err_class )           @exception = ex_class.new( err_msg )         end       end     end   end end 

This means that if you get an errorcode like AWS.InvalidParameterValue, this will produce (in its exception variable) a new class Amazon::AWS::Error::InvalidParameterValue which is a subclass of StandardError.

Now here’s where it gets weird. I have some code that looks like this:

begin   do_aws_stuff rescue Amazon::AWS::Error => error   puts 'Got an AWS error' end 

Now, if do_aws_stuff throws a NameError, my rescue block gets triggered. It seems that Amazon::AWS::Error isn’t the superclass of the generated error – I guess since it’s a module everything is a subclass of it? Certainly if I do:

irb(main):007:0> NameError.new.kind_of?(Amazon::AWS::Error) => true 

It says true, which I find confusing, especially given this:

irb(main):009:0> NameError.new.kind_of?(Amazon::AWS) => false 

What’s going on, and how am I supposed to separate out AWS errors from other type of errors? Should I do something like:

begin   do_aws_stuff rescue => error   if error.class.to_s =~ /^Amazon::AWS::Error/     puts 'Got an AWS error'   else     raise error   end end 

That seems exceptionally janky. The errors thrown aren’t class AWSError either – they’re raised like this:

error = Amazon::AWS::Error::AWSError.new( xml ) raise error.exception 

So the exceptions I’m looking to rescue from are the generated exception types that only inherit from StandardError.

To clarify, I have two questions:

  1. Why is NameError, a Ruby built in exception, a kind_of?(Amazon::AWS::Error), which is a module?
    Answer: I had said include Amazon::AWS::Error at the top of my file, thinking it was kind of like a Java import or C++ include. What this actually did was add everything defined in Amazon::AWS::Error (present and future) to the implicit Kernel class, which is an ancestor of every class. This means anything would pass kind_of?(Amazon::AWS::Error).

  2. How can I best distinguish the dynamically-created exceptions in Amazon::AWS::Error from random other exceptions from elsewhere?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:57:07+00:00Added an answer on May 10, 2026 at 2:57 pm

    Ok, I’ll try to help here :

    First a module is not a class, it allows you to mix behaviour in a class. second see the following example :

    module A   module B     module Error       def foobar         puts 'foo'       end     end   end end  class StandardError   include A::B::Error end  StandardError.new.kind_of?(A::B::Error) StandardError.new.kind_of?(A::B) StandardError.included_modules #=> [A::B::Error,Kernel] 

    kind_of? tells you that yes, Error does possess All of A::B::Error behaviour (which is normal since it includes A::B::Error) however it does not include all the behaviour from A::B and therefore is not of the A::B kind. (duck typing)

    Now there is a very good chance that ruby-aws reopens one of the superclass of NameError and includes Amazon::AWS:Error in there. (monkey patching)

    You can find out programatically where the module is included in the hierarchy with the following :

    class Class   def has_module?(module_ref)     if self.included_modules.include?(module_ref) and not self.superclass.included_modules.include?(module_ref)                               puts self.name+' has module '+ module_ref.name               else       self.superclass.nil? ? false : self.superclass.has_module?(module_ref)     end           end end StandardError.has_module?(A::B::Error) NameError.has_module?(A::B::Error) 

    Regarding your second question I can’t see anything better than

    begin  #do AWS error prone stuff rescue Exception => e   if Amazon::AWS::Error.constants.include?(e.class.name)     #awsError   else     whatever   end  end 

    (edit — above code doesn’t work as is : name includes module prefix which is not the case of the constants arrays. You should definitely contact the lib maintainer the AWSError class looks more like a factory class to me :/ )

    I don’t have ruby-aws here and the caliban site is blocked by the company’s firewall so I can’t test much further.

    Regarding the include : that might be the thing doing the monkey patching on the StandardError hierarchy. I am not sure anymore but most likely doing it at the root of a file outside every context is including the module on Object or on the Object metaclass. (this is what would happen in IRB, where the default context is Object, not sure about in a file)

    from the pickaxe on modules :

    A couple of points about the include statement before we go on. First, it has nothing to do with files. C programmers use a preprocessor directive called #include to insert the contents of one file into another during compilation. The Ruby include statement simply makes a reference to a named module. If that module is in a separate file, you must use require to drag that file in before using include.

    (edit — I can’t seem to be able to comment using this browser :/ yay for locked in platforms)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try this (from SQL2000 help): RESTORE DATABASE dbname WITH RECOVERY May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer Just a wild guess, could you try to define your… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer You forgot your select; try this: var result = from… May 11, 2026 at 11:55 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.