user1611830
user1611830

Reputation: 4867

fetching instances from rescue/raise error

Suppose I have a gem where some method is called

      def grit
          @grit ||= Grit::Repo.new(path)
        rescue Grit::NoSuchPathError
          raise NoRepository.new('no repository for such path')
      end

Suppose now I call from my rails app this method. Is there a way to fetch this NoRepository instance from my app or am I supposed to change this method in order to make it return this instance ?

Upvotes: 0

Views: 135

Answers (2)

Simone Carletti
Simone Carletti

Reputation: 176472

NoRepository is not an instance of a repository, is an exception raised when path don't reference a valid git repository.

That method should work exactly as it is.

If for whatever reason you need to get an instance of the repo without passing by the grit helper, you can call directly

 Grit::Repo.new(path)

passing a path, for example

 Grit::Repo.new('/tmp/foo')

Upvotes: 0

vee
vee

Reputation: 38645

I'm unsure what you mean by fetch NoRepository instance. Basically the way you reference that instance of NoRepository is by rescuing from that error.

The code calling the grit method and rescuing from NoRepository:

begin
  grit()
rescue NoRepository => nr
  # Here `nr` is the instance of NoRepository
end

Upvotes: 1

Related Questions