Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper functions not available in rescue_from #438

Closed
skwp opened this issue Jul 17, 2013 · 19 comments
Closed

Helper functions not available in rescue_from #438

skwp opened this issue Jul 17, 2013 · 19 comments

Comments

@skwp
Copy link

skwp commented Jul 17, 2013

Hi, we are trapping errors in rescue_from but unable to log them because the logger variable appears out of scope. Any ideas on how to access it? Thanks.

@skwp
Copy link
Author

skwp commented Jul 17, 2013

I was able to get around this by defining a constant. It's a bit ugly but it works...something like this:

module API
   LOGGER = Logger.new(...)
   logger LOGGER

   rescue_from :all do |e|
     LOGGER.error e
   end
end

@skwp skwp closed this as completed Jul 17, 2013
@ismaelga
Copy link

I think you should leave it open. I think logger should be available there without hacks

@aac
Copy link

aac commented Jul 17, 2013

none of the helpers are available in rescue_from either, no?

On Wed, Jul 17, 2013 at 8:45 AM, Ismael Abreu notifications@github.comwrote:

I think you should leave it open. I think logger should be available
there without hacks


Reply to this email directly or view it on GitHubhttps://github.com//issues/438#issuecomment-21110122
.

@skwp
Copy link
Author

skwp commented Jul 17, 2013

correct, no helpers are available there. I think it's a bit strange.

@skwp skwp reopened this Jul 17, 2013
@ecielam
Copy link

ecielam commented Aug 28, 2013

+1 Fixing this would be GREAT.

@col
Copy link

col commented Sep 30, 2013

+1

@sheerun
Copy link

sheerun commented Dec 18, 2013

+1, the error! is not available, so I can't pass error code

@kamui
Copy link

kamui commented May 2, 2014

+1 I just ran into this issue as well, trying to access error! and a helper wrapping error! from a rescue_from block.

@schultyy
Copy link

+1
Also ran into this error today.

@cmaitchison
Copy link

You can access helpers, etc from a rescue_from like this:

rescue_from StandardError do |e|
  env['api.endpoint'].helper_method
end

@jmondo
Copy link

jmondo commented Sep 26, 2014

@cmaitchison that tip was super helpful thanks!

@jeremyhaile
Copy link

Thanks for the workaround. This is a huge pain. Please fix!

@thimios
Copy link

thimios commented Apr 20, 2015

Would be great to fix this, also the workaround does not work for me:

class App < Grape::API
  rescue_from ActiveRecord::RecordNotFound do |e|
      env['api.endpoint'].error!( e.message, 404 )
  end
end

Gives:

ArgumentError: uncaught throw :error

any ideas?

thanks!

@aserafin
Copy link
Contributor

I'm using it like this https://github.com/aserafin/grape_logging#logging-exceptions. You can also checkout the gem itself - it provides you with basic similar to Rails logging features.

@jamesmoriarty
Copy link

@cmaitchison life saver.

@flocsy
Copy link

flocsy commented Oct 15, 2015

+1, logger is essential in the whole code, especially in rescue_from. It's a pity it wasn't solved yet after more than 2 years :(

@dblock
Copy link
Member

dblock commented Oct 15, 2015

@flocsy Make a PR?

@zuk
Copy link

zuk commented Nov 2, 2015

Just for the record, error! is available in rescue_from blocks since 0.12.0. See #974

Other/custom helpers however still aren't.

@dblock
Copy link
Member

dblock commented Dec 26, 2015

This was fixed on HEAD, thanks @namusyaka.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.