In my ApiController I have:
rescue_from Exception, with: :render_exception_error
def render_exception_error(exception)
raise exception if Rails.env.test?
logger.error(exception)
render json: { error: 'An error occurred' }, status: 500 unless performed?
end
I want that method to also report the error to Sentry.
Do you know if by doing the line that does logger.error(...) it automatically logs it in Sentry too?
Or do I have to manually do Raven.capture_exception(exception) or something like that? Will this be done in the background?
Edit 2022
Raven is deprecated since a few months now and you should use the new Sentry gem.
To answer the question, which is Report errors manually from Rails to Sentry, you can check the documentation here: https://docs.sentry.io/platforms/ruby/guides/rails/usage/
TL;DR you can capture exception with
Sentry.capture_exception(exception)
Or you can capture a message with
Sentry.capture_message('Something went wrong')
Related
I have done this tons of times before when fetching things from a database, etc.
For my specific case I am using a 3rd party to connect to a piece of hardware... Anyways, in the case of an error, such as an invalid id obviously, we want to raise a exception or a rescue... but unfortunately I don't know how to raise it because by the time it is hit, it's too late (I think)
Here...
#
# getting params and saving item above...
#
if item.save
device = RubySpark::Device.new("FAKEUNITID800")
device.function("req", "ITEM")
redirect_to controller: 'items', action: 'edit_items'
end
If this was a valid ID, everything would work, and it would take you to the /edit page! But the issue is, with an invalid ID, it just does...
Completed 500 Internal Server Error in 897ms
RubySpark::Device::ApiError - Permission Denied: Invalid Device ID:
I checked out the following tutorials
Rescue StandardError, Not Exception
How to catch 404 and 500 error in Rails?
Dynamic Rails Error Pages
But honestly, they just make me more confused. Maybe I have the wrong approach to this. I always thought that first you make the request, and then you have a fall back case, depending what status (ie. 200, 500, 404) you get... you go from there.
Rails returns an 500 Internal Server Error response because an exception was raised that it does not no how to handle. You can't rescue "500 Internal Server Error" in Rails because it is not an exception - its the framework bailing from an uncaught exception to avoid data loss or unpredictable behavior.
Fortunatly you don't have to. You can just rescue the RubySpark exception:
begin
device = RubySpark::Device.new("FAKEUNITID800")
device.function("req", "ITEM")
rescue RubySpark::Device::ApiError => e
logger.error(e.message)
end
You can also use rescue_from in Rails controllers that wraps the entire action in a before block:
class FooController < ApplicationCotnroller
rescue_from RubySpark::Device::ApiError, with: :do_something
# ...
end
I am new to Rails and Nginx. I was asked to add custom error pages to our app. I found instructions to do so on this site and my custom error pages render just fine locally. When I go to the url /no_such_page, I see the appropriate page. When I deploy the code to our test server running nginx and try the same url, I see the following instead:
500 Internal Server Error
If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.`
On the nginx server, if I go to the url \404, then I do see my page, so I know it renders ok.
Both my local machine and the server are running under the development environment.
I've added this to config\development.rb:
config.consider_all_requests_local = false
config.exceptions_app = self.routes
Here is what I've added to routes.rb:
%w(404 500).each do |code|
get code, to: "errors#show", :code => code, :via => :all
end
and my errors_controller looks like this:
class ErrorsController < ApplicationController
def show
status_code = params[:code] || 500
render status_code.to_s
end
end
Does anyone know if there is something special that I need to do in my nginx config to make this work?
500 Internal Server Error If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.
This error message is from Rails itself, not from Nginx.
https://github.com/rails/rails/blob/v5.0.0.1/actionpack/lib/action_dispatch/middleware/show_exceptions.rb#L15-L22
It means that your custom error page is itself raising an exception, which would lead to an infinite loop of error->show error page->error->show error page->... Rails detects this and halts with the error message you are seeing. Check your logs for this string to see what the error is:
Error during failsafe response:
I'm on Sinatra and i don't understand how to deal with my problem:
I want to "send" to curl a custom message when he try to go on a wrong path.
curl http://localhost:port/blabla
It's an error 404 but i want to send him thing like 'error try other path'
I tried with this :
before'/*' do
if (params[:splat].to_s =~ /path_i_want/) != 2
'wrong path'
end
end
or with raise 404 but it doesn't work.
Could you help me please ?
Regards.
Sinatra has a built-in handler for 404, see Error Handling page. You could do all your logic in there.
Not Found
When a Sinatra::NotFound exception is raised, or the
response’s status code is 404, the not_found handler is invoked:
not_found do
'This is nowhere to be found.'
end
I'm looking at the mandrill docs for a ruby api call https://mandrillapp.com/api/docs/users.ruby.html#method=ping and I noticed that they rescue Mandrill::Error and then raise another exception.
I'm curious why anyone would ever catch one exception and then raise another one. It doesn't make sense to me.
begin
mandrill = Mandrill::API.new 'YOUR_API_KEY'
result = mandrill.users.ping
# {"PING"=>"PONG!"}
rescue Mandrill::Error => e
# Mandrill errors are thrown as exceptions
puts "A mandrill error occurred: #{e.class} - #{e.message}"
# A mandrill error occurred: Mandrill::InvalidKeyError - Invalid API key
raise
end
rescue Mandrill::Error => e
# Mandrill errors are thrown as exceptions
puts "A mandrill error occurred: #{e.class} - #{e.message}"
# A mandrill error occurred: Mandrill::InvalidKeyError - Invalid API key
raise
end
In this case the same exception is being "re-raised". The only reason for this rescue block is to log specific information about the exception.
begin/rescue blocks are usually used this way when an exception is of special interest and so the authors wanted the exception info to be printed/logged. This is especially the case when the next rescue block is not printing any exception information and instead silently handling it.
raise will throws a runtime exception instead of a normal exception.
Another reason for doing it could be that code wanted to throw a runtime error instead of a normal error. Maybe relevant if the code is in the context of a controller.
However, I think the main reason will be Martin's logging answer.
I'm using Google API Ruby Client (gem 'google-api-client') in a Rails Web app, and I'd like to know how to catch specific errors in the oauth flow. In particular, what should I look for in the rescue statement? Here's the function called by the redirect after the user authorizes:
require 'google/api_client'
def google_auth_finish
begin
client = Google::APIClient.new
client.authorization.client_id = GOOGLE_CLIENT_ID
client.authorization.client_secret = GOOGLE_CLIENT_SECRET
...
rescue ## WHAT GOES HERE TO IDENTIFY THE ERROR?
# Handle the error
logger.info "There was an error."
end
end
Is there a reference somewhere with defined errors? I've searched and can't find it.
I know this was asked years ago, but I literally just encountered this problem and happened upon this question. You were just missing a small part. This worked for me. I am still relatively new, but in my case it prevented the program from breaking and printed out the error message, then the program continued on.
rescue Exception => error
puts "Error #{error}"
end
try theseRaising An Exception