Defining custom exception in a module in Rails
exception, module, ruby-on-rails
Solution
Nevermind, i solved the problem myself:
instead of using the `if ? then : else` statement, i expanded it into a:
raise LibTH::Error::IDNotFound if res.size == 0
res
It seems you can't use a constant value (as a class name) in the C-like `if` statement, thus the:
syntax error, unexpected tCONSTANT, expecting kDO or '{' or '('
Thanks asy!
Problem
I've created a custom module (which currently only defines a new Exception class), and put it under `lib/lib_th.rb` ``` module LibTH module Error IDNotFound = Class.new end end ``` I should not need to require/include the module in my code, as it should be automatically loaded, since it follows the conventional naming rules. But when i try and raise the `IDNotFound` exception somewhere in my code: ``` res.size == 0 ? raise LibTH::Error::IDNotFound : res ``` I get the follwoing error: ``` SyntaxError (/Users/lrnz/code/ruby/corinna/app/models/treasure_hunt.rb:49: syntax error, unexpected tCONSTANT, expecting kDO or '{' or '(' res.size == 0 ? raise LibTH::Error::IDNotFound : res ^ /Users/lrnz/code/ruby/corinna/app/models/treasure_hunt.rb:49: syntax error, unexpected ':' res.size == 0 ? raise LibTH::Error::IDNotFound : res ^): app/controllers/treasure_hunts_controller.rb:50:in `show' ``` The strange thing is that I encounter no problems trying to raise the exception in `script/console`: ``` >> raise LibTH::Error::IDNotFound LibTH::Error::IDNotFound: LibTH::Error::IDNotFound from (irb):70 ``` Thanks!