I wrote a simple ROR application and i raised an exception using airbrake. This is my code as follows
require 'airbrake'
require 'config/initializers/airbrake'
begin
raise "Serious problems happened"
params = {
:api_key => Airbrake.api_key,
:error_message => 'Notification',
:backtrace => caller,
:parameters => {},
:session => {}
}
rescue => e
Airbrake.notify(:error_class => "Special Error", :error_message => "Spe
cial Error: #{e.message}", :parameters => params)
end
When i run the above code, i'm getting the following exception
/root/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/cus
tom_require.rb:55:in `require': no such file to load -- config/initializers/airb
rake (LoadError)
from /root/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1
/rubygems/custom_require.rb:55:in `require'
from sample.rb:2:in `<main>''
But i have airbrake.rb in config/initializers, I don't know why the path is not taken.
What could be the issue here
You don't generally require your initializers explicitly. Code in config/initializers/ gets automatically loaded by your application.
Furthermore, if you've set up your Airbrake initializer properly, you can use a handy little helper called notify_airbrake to fire off Airbrake traces:
begin
...
rescue Exception => e
notify_airbrake e
end
That's all there is to it!
Related
Following the tutorial, I created a file omniauth.rb at the path
spec/support/helpers/omniauth.rb
module Omniauth
module Mock
def auth_mock
OmniAuth.config.mock_auth[:twitter] = {
'provider' => 'twitter',
'uid' => '123545',
'user_info' => {
'name' => 'mockuser'
},
'credentials' => {
'token' => 'mock_token',
'secret' => 'mock_secret'
}
}
end
end
end
But when I run rspec, I get an error with "uninitialized constant Omniauth"
rails-omniauth/spec/support/helpers.rb:2:in `block in <top (required)>': uninitialized constant Omniauth (NameError)
It seems clear that either omniauth.rb or helpers.rb should be in a different location, but I don't know where.
Update:
I subsequently tried installing the rails-omniauth via the Rails Composer app. When I run "rspec" for this app, I get exactly the same error.
At one point in the tutorial you are given a choice between creating a file at at /spec/support/helpers.rb:
RSpec.configure do |config|
config.include Omniauth::Mock
config.include Omniauth::SessionHelpers, type: :feature
end
OmniAuth.config.test_mode = true
Or adding these same lines to /spec/rails_helper.rb.
I created the new file at /spec/support/helpers.rb. To make this work, I needed to add the line require_relative 'helpers/omniauth' at the top of the file. The Rails Composer app also adds the helpers.rb file rather than editing rails_helper.rb, so the same line is needed to make rspec run successfully for that app.
In my Rails 3 app, I am fetching path_info by:
Rails.application.routes.recognize_path(url, { :method => request.request_method }) rescue {}
If a crawler hits a URL like "http://localhost:3000/admin_", the above code raises following error:
LoadError: Expected /Users/user/myRailsApp/app/controllers/admin_controller.rb to define Admin_Controller
from /Users/user/.rvm/gems/ree-1.8.7-2012.02/gems/activesupport-3.0.20/lib/active_support/dependencies.rb:492:in `load_missing_constant'
I have two questions:
Why is rescue not working? If I change it to rescue LoadError => e, exception is handled gracefully.
Is there any other alternative rather than rescuing such exception(s)?
If you omit the exception type, by default rescue will rescue only StandardError exceptions and subclasses.
LoadError doesn't inherit from StandardError:
LoadError.ancestors
=> [LoadError, ScriptError, Exception, Object, Kernel, BasicObject]
Therefore, the one-line rescue pattern doesn't work with a LoadError.
I have a Ruby on Rails app with an API in lib. Files in lib are autoloaded, and the API is configured in an initializer.
# lib/my_api.rb
module MyApi
extend Configuration
end
# lib/my_api/configuration.rb
module MyApi
module Configuration
attr_accessor :my_setting
def configure
yield self
end
end
end
# config/initializers/my_api.rb
MyApi.configure do |config|
config.my_setting = 'foo'
end
This works in production, but in development the API gets configured when the server is started. After I change some code, the configuration is lost and there are errors because the settings are nil:
irb(main):001:0> MyApi.my_setting
=> "foo"
irb(main):002:0> reload!
Reloading...
=> true
irb(main):003:0> MyApi.my_setting
=> nil
My guess is that in development, the classes are reloaded, but the initializer is not, which means it only gets configured once after starting the server.
Right now I'm duplicating my configuration in lib/my_api.rb, but that's very hacky.
What's a clean solution for this problem?
module MyApi
module Configuration
mattr_accessor :my_setting
def configure
yield self
end
end
end
mattr_accessor is an ActiveSupport macro for creating module level accessors.
Well, until someone comes up with a better solution, I've come up with two workarounds. I went with 2.
Don't autoload the lib directory (meaning don't autoload the API). That means having to restart the server when the API code changes, but solves the problem. That's why configuring gems like this works -- because they aren't autoloaded.
Manually reload the initializer in development at the end of lib/my_api.rb:
load Rails.root.join('config/initializers/smart_meter.rb') if Rails.env.development?
The MyApi constant will be replaced by a new one when Rails autoloads classes. The configuration is still available on the old object:
Loading development environment (Rails 4.2.0)
irb: warn: can't alias context from irb_context.
irb(main):001:0> MyApi.my_setting
=> "foo"
irb(main):002:0> OldMyApi = MyApi
=> MyApi
irb(main):003:0> reload!
Reloading...
=> true
irb(main):004:0> MyApi.my_setting
=> nil
irb(main):005:0> OldMyApi.my_setting
=> "foo"
irb(main):006:0> load Rails.root.join('config/initializers/smart_meter.rb')
=> true
irb(main):007:0> MyApi.my_setting
=> "foo"
I have this code which is in lib folder. This code works outside of rails, but when it's called from the rails controller I get the uninitialized constant AWS::S3::Base error
require 'rubygems'
require 'aws/s3'
module S3Util
def self.upload_file(local_file)
mime_type = "application/octet-stream"
bucket = "test"
AWS::S3::Base.establish_connection!(
:access_key_id => '*****',
:secret_access_key => '****'
)
base_name = File.basename(local_file)
puts "**** Uploading #{local_file} as '#{base_name}' to '#{bucket}'"
AWS::S3::S3Object.store(
base_name,
File.open(local_file),
bucket,
:content_type => mime_type
)
puts "***** Uploaded!"
end
end
just do in your controller
require 'aws/s3'
and its work for me
Rails doesn't "know" that the module is available to the application; you have to add it to the paths it looks in. You can do that in a couple of ways, but most people do the following.
Add this line to your config/application.rb:
config.autoload_paths += Dir["#{config.root}/lib/**/"]
You may, at some point, want to be more specific about which directories are searched, but this should get you going.
Andrew
I am new for ROR Developer. i have one table to insert car images. but, that images are remote url. I have to insert 60,000 rows. i got like this "error execution terminated". Can you help how do i fix this issue?
Here My Code:
namespace :db do
task :load_photo => :environment do
require 'rubygems'
require 'open-uri'
require 'net/http'
require 'paperclip'
Website.find_in_batches(:conditions=>["image_url is not null"]) do |websites|
websites.each do |website|
begin
url = URI.parse(website.image_url)
Net::HTTP.start(url.host, url.port) do |http|
if http.head(url.request_uri).code == "200"
Car.update_attribute(:photo,open(url))
end
end
rescue Exception => e
end
end
end
end
end
I would suggest you to not rescue all Exception like you did with :
rescue Exception => e
end
then you will have (and be able to give us) more information about the error generated.
Notice that it is a good practice to rescue only exception you want.