0

I am relatively new to ruby and I am stuck with this problem which is rather hard to solve.

What I want to achieve is that I could catch custom errors which I throw from the sub class in a parent class. Given an example below, how could I make the parent class to understand the RequestTimeout class? Because, now when I run the code it results to a following output:

test_raise.rb:5:in `rescue in handle_errors': uninitialized constant BaseService::RequestTimeout (NameError) from test_raise.rb:4:in `handle_errors' from test_raise.rb:14:in `first_service_method' from test_raise.rb:31:in `<main>' 

The code:

class BaseService def handle_errors yield rescue RequestTimeout => e # <-- the problem p e.message end end class FirstService < BaseService class RequestTimeout < StandardError; end def first_service_method handle_errors do raise RequestTimeout, "FirstService RequestTimeout" end end end class SecondService < BaseService class RequestTimeout < StandardError; end def second_service_method handle_errors do raise RequestTimeout, "SecondService RequestTimeout" end end end a = FirstService.new a.first_service_method 

Ofc. I could solve the problem by changing:

rescue RequestTimeout => e 

to:

rescue => e 

But I dont want to do that because I wan't to catch multiple exceptions (more than RequestTimeout) which are defined and raised by me. Any help would be awesome!

2
  • 2
    Move class RequestTimeout < StandardError; end into base class. It will become available in all children. Whether you have different exceptions for different child classes, you are forced to use namespaces, as @marek-lipka said below. Commented Nov 19, 2014 at 8:33
  • @mudasobwa Yea, thats what I ended up doing - I worried about doing it earlier as it seemed that rails rescue_from catched errors which wasn't from the class it was throwing from. However, I am having trouble of repeating that. So If you are so kind and post your comment as an answer I accept that. Commented Nov 19, 2014 at 8:38

2 Answers 2

1

There is an option to move

class RequestTimeout < StandardError; end 

declaration into the base class. It hence will become available to all children.

Whether you have different exceptions for different child classes, you are forced to use namespaces, as @Marek said in sibling comment.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is about namespaces - RequestTimeout is defined in different namespace than BaseService. You should have:

rescue FirstService::RequestTimeout => e 

2 Comments

Thanks for your answer, but if I have 10 or more classes extending base service you could see how bad the code is going to look like in the BaseService? :S rescue FirstService::RequestTimeout .. SecondService::RequestTimeout ... etc.. is there are other way? Like dynamically accessing X::RequestTimeout or similar? How I can teach upper class to understand that he owns RequestTimeout? :( Moreover, if BaseService is provided by API and sub class (3rd party) is using it there is no change the API provider would modify the upper class all the time :/ ?
I came to in conclusion that what I wanted to do in my brains wasn't possible with ruby. So I am giving you +1 for the effort because I can't deny that you are wrong either. Best possible solution however were the one which @mudasobwa pointed out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.