Mongoid Error Redirection

100 Views Asked by At

I would like to be able to redirect the output raised from an error from mongoid. I have some simple code in the following layout:

begin
  [ruby code here to query database]
rescue [Moped Exception here] => e
  puts e.message
end

When an error occurs, I get it in the following format:

Moped::Errors::ConnectionFailure: Could not connect to a primary node for replica set <Moped::Cluster nodes=[<Moped::Node resolved_address=”ipAddress”>, <Moped::Node resolved_address=”ipAddress”>]>

This error is displayed several times before the exception is actually caught and displayed. I'm okay with the fact that the error occurs, but I would like to hide the error output above/not display it, and only display the exception. Is there some way to redirect the error output raised from the ConnectionFailure error. Or a way to configure mongoid such that error messages are not displayed (I'm running from the console)? Any way to redirect the errors would be helpful.

1

There are 1 best solutions below

0
njny On

The mongoid documents say you shouldn't try rescuing from ConnectionFailure: http://mongoid.org/en/moped/docs/driver.html#errors

Besides for this, you say you don't care if it occurs. What code have you tried for redirecting the user? Also what are you using (Rails, Sinatra, etc.)?

For Rails I usually do the following:

  if !request.env["HTTP_REFERER"].blank? and request.env["HTTP_REFERER"] != request.env["REQUEST_URI"]
   redirect_to :back
 else
   redirect_to root_path
 end

This will try to send the user back to their previous path if possible (if it was stored), else it will bring them to root.