ConradIrwin / pry-rescue

Start a pry session whenever something goes wrong.
MIT License
850 stars 49 forks source link

Compatible with pry-remote? #37

Open findchris opened 11 years ago

findchris commented 11 years ago

Hi there, and thanks for pry et. al. Great work.

Can pry-rescue be used with pry-remote?

In other words, with pry-remote you currently have to explicitly call binding.remote_pry, but with pry-rescue, the pry session is entered directly. It would be great to use these gems synergistically.

Cheers.

PS. I'm cross-posting this question to the pry-remote issue tracker as well.

ConradIrwin commented 11 years ago

Not yet, but I think it wouldn't be hard to change. If you want to send a pull request, feel free; otherwise it'll go on my queue :).

findchris commented 11 years ago

I can take a stab at it. Can you point me in the general direction of where you imagine the changes would take place? The world of callers, bindings, and frames is slowly becoming more familiar to me ;-)

ConradIrwin commented 11 years ago

Awesome :). It should just be a case of fixing this: https://github.com/ConradIrwin/pry-rescue/blob/master/lib/pry-rescue.rb#L44

Not sure how you want to configure it, maybe the user has to do something like Pry.config.rescue_remotely = true?

findchris commented 11 years ago

Just a quick update. I've made some good progress on this, but I am waiting on the following pull requests to pry-remote to be accepted, as they are required by the integration from what I can tell:

Stay tuned.

benritchie commented 10 years ago

Did you get anywhere an this? This would be an awesome feature to have...

findchris commented 10 years ago

I did make some progress, and it was working well, but I never got it to a state of being production-ready. If there's interest, I can dig through my code to see where I left off...

benritchie commented 10 years ago

I'd definately use this. - lots of my code can't be run in development mode for various reasons.
I don't think I can help much with the changes, but would happily test.


From: Chris Johnson [notifications@github.com] Sent: 19 July 2014 05:22 To: ConradIrwin/pry-rescue Cc: Ben Ritchie Subject: Re: [pry-rescue] Compatible with pry-remote? (#37)

I did make some progress, and it was working well, but I never got it to a state of being production-ready. If there's interest, I can dig through my code to see where I left off...

— Reply to this email directly or view it on GitHubhttps://github.com/ConradIrwin/pry-rescue/issues/37#issuecomment-49499267.

aeberlin commented 8 years ago

Stick this in an initializer:

class PryRescue
  class << self
    # Start a Pry session in the context of the exception.
    # @param [Exception] exception  The exception raised
    def enter_exception_context(exception)
      @any_exception_captured = true
      @exception_context_depth ||= 0
      @exception_context_depth += 1

      exception = exception.instance_variable_get(:@rescue_cause) if phantom_load_raise?(exception)
      bindings = exception.instance_variable_get(:@rescue_bindings)

      bindings = without_bindings_below_raise(bindings)
      bindings = without_duplicates(bindings)

      with_program_name "#$PROGRAM_NAME [in pry-rescue @ #{Dir.pwd}]" do
        if defined?(PryStackExplorer)
          pry :call_stack => bindings,
              :hooks => pry_hooks(exception),
              :initial_frame => initial_frame(bindings)
        else
          # Pry.start bindings.first, :hooks => pry_hooks(exception)
          PryRemote::Server.new(
            bindings.first,
            PryRemote::DefaultHost,
            PryRemote::DefaultPort,
            hooks: pry_hooks(exception)
          ).run
        end
      end
    ensure
      @exception_context_depth -= 1
    end
  end
end

Rails.application.middleware.use PryRescue::Rack
zw963 commented 3 years ago

Previous code not works if used with pry-stack_explorer gem, following code fix this, and can auto check if current process if run with Procfile's tools as daemon or run directly on console, and decide if use pry-remote conditionally.

class PryRescue
  class << self
    def enter_exception_context(exception)
      @any_exception_captured = true
      @exception_context_depth ||= 0
      @exception_context_depth += 1

      exception = exception.instance_variable_get(:@rescue_cause) if phantom_load_raise?(exception)
      bindings = exception.instance_variable_get(:@rescue_bindings)

      bindings = without_bindings_below_raise(bindings)
      bindings = without_duplicates(bindings)

      with_program_name "#$PROGRAM_NAME [in pry-rescue @ #{Dir.pwd}]" do
        notify_send('loading remote pry ...')
        if defined?(PryStackExplorer)
          if $stdout.tty?
            pry :call_stack => bindings,
              :hooks => pry_hooks(exception),
              :initial_frame => initial_frame(bindings)
          else
            PryRemote::Server.new(
              nil,
              PryRemote::DefaultHost,
              PryRemote::DefaultPort,
              :call_stack => bindings,
              :hooks => pry_hooks(exception),
              :initial_frame => initial_frame(bindings)
            ).run
          end
        else
          if $stdout.tty?
            Pry.start bindings.first, :hooks => pry_hooks(exception)
          else
            # 这里做的 hack, 让 pry-rescue 使用 pry-remote
            PryRemote::Server.new(
              bindings.first,
              PryRemote::DefaultHost,
              PryRemote::DefaultPort,
              hooks: pry_hooks(exception)
            ).run
          end
        end
      end
    ensure
      @exception_context_depth -= 1
    end
  end
end