Ruby `require` call fails on custom code
The current directory (relative to the main ruby file) is not part of the load paths (the set of paths that require
looks in). So you either need to add it to the load path (best done by invoking ruby as ruby -I. test1.rb
), by using require "./test2.rb"
or by using require_relative "test2.rb"
, which requires files relative to the directory of the file.
Dealing with Custom Exceptions Codes from Failed API Call
As Dave said, if the exception is all you've got, you can only capture the exception and do something with that exception as a string.
For example:
begin
...
rescue Exception => ex
# here "ex.message" contains your string, you can do anything you want with it
# or parse as is:
flash[:notice] = ex.message
end
redirect_to edit_user_registration_path
UPDATE
If you combine my proposed solution with those put forward by Dave, you'd get something like this, with which you can use your own error strings:
begin
...
rescue Exception => ex
code = ex.message.match(/.*?- (\d+): (.*)/)[1]
case code
when '172'
flash[:notice] = 'Please wait 30 minutes to create more clients'
end
end
redirect_to edit_user_registration_path
Ruby 'require' error: cannot load such file
I just tried and it works with require "./tokenizer"
. Hope this helps.
Load error when trying to include custom module
Assuming you're using Ruby 1.9, the default $LOAD_PATH
no longer includes the current directory. So while statements like require 'sinatra'
work just fine (because those gems are in $LOAD_PATH
), Ruby doesn't know that your lib/authorization
file is located relative to your main Sinatra file.
You can add the Sinatra file's directory to the load path, and then your require
statements should work fine:
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'sinatra'
require 'rubygems' # Not actually needed on Ruby 1.9
require 'datamapper'
require 'dm-core'
require 'lib/authorization'
Dealing with Custom Exceptions Codes from Failed API Call
As Dave said, if the exception is all you've got, you can only capture the exception and do something with that exception as a string.
For example:
begin
...
rescue Exception => ex
# here "ex.message" contains your string, you can do anything you want with it
# or parse as is:
flash[:notice] = ex.message
end
redirect_to edit_user_registration_path
UPDATE
If you combine my proposed solution with those put forward by Dave, you'd get something like this, with which you can use your own error strings:
begin
...
rescue Exception => ex
code = ex.message.match(/.*?- (\d+): (.*)/)[1]
case code
when '172'
flash[:notice] = 'Please wait 30 minutes to create more clients'
end
end
redirect_to edit_user_registration_path
Rspec failing on custom exception
You need to put your code in a block so that RSpec can evaluate it, as in:
expect { raise Test::BadError }.to raise_exception( Test::BadError )
When you pass it as a parameter, the error gets raised before RSpec do anything.
How to use custom error messages
Simply modify your script like this.
class MyCustomError < StandardError
def message
"oops... That was not a number"
end
end
def print_a_number
begin
puts "chose a number"
number = Float(gets) rescue false
raise MyCustomError unless number.is_a? Numeric
puts "The number you chose is #{number}"
rescue MyCustomError => err
puts err.message
puts err.backtrace.inspect
end
end
Related Topics
Get All Keys in Hash with Same Value
Why Does the Rand() Return Always the Same Number
In Rspec, Using Let Variable Inside Before :All Block
How to Access a Symbol Hash Key Using a Variable in Ruby
How to Optimize Graphviz Output Width
Ruby Class Object Garbage Collection
How to Mock Super in Ruby Using Rspec
Why Is 032 Different Than 32 in Ruby
Show Markers on Google Maps Dynamically -Rails 3.2
Validate That String Contains Only Allowed Characters in Ruby
Parsing Large Xml with Nokogiri
How to Create an Operator for Deep Copy/Cloning of Objects in Ruby
Does Anyone Have Parsing Rules for the Notepad++ Function List Plugin for Ruby and Rake
More Concise Version of Max/Min Without the Block
Algorithm to Print All Valid Combations of N Pairs of Parenthesis
Ruby Converting Utc to User's Time Zone
How to Make a Ruby Script Using Trollop for Command Line Parsing