Class: Git::SystemCallGuard Private

Inherits:
Object
  • Object
show all
Defined in:
lib/git/system_call_guard.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Convert SystemCallError raised by the gem's own filesystem calls to Error

The gem promises to raise only ArgumentError or a subclass of Error. Filesystem calls such as File.read, Dir.chdir, or Tempfile.create can raise a bare SystemCallError (Errno::ENOENT, Errno::EACCES, and so on). Wrapping those calls in SystemCallGuard.call keeps the promise: the SystemCallError is re-raised as a Error with the original error available as cause.

Helpers that yield to caller-supplied blocks must not relabel errors raised by the caller's own code. #unguarded marks that region so a SystemCallError raised inside it propagates unchanged.

Examples:

Guard a single filesystem call

content = Git::SystemCallGuard.call('Failed to read the gitdir pointer file') do
  File.read(path)
end

Guard setup and cleanup but not the caller's block

def with_tempfile(&block)
  Git::SystemCallGuard.call('Failed to create a temporary file') do |guard|
    Tempfile.create do |file|
      guard.unguarded { block.call(file) }
    end
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(message) {|guard| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run the block, converting any SystemCallError it raises to Error

Parameters:

  • message (String) —

    prefix for the Error message

Yields:

  • (guard) —

    the guarded region

Yield Parameters:

Yield Returns:

  • (Object) —

    returned as the method's return value

Returns:

  • (Object) —

    the value returned by the block

Raises:

  • (Git::Error) —

    if the block raises a SystemCallError outside an unguarded region



50
51
52
# File 'lib/git/system_call_guard.rb', line 50

def self.call(message, &)
  new.call(message, &)
end

Instance Method Details

#call(message) {|guard| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run the block, converting any SystemCallError it raises to Error

Parameters:

  • message (String) —

    prefix for the Error message

Yields:

  • (guard) —

    the guarded region

Yield Parameters:

Yield Returns:

  • (Object) —

    returned as the method's return value

Returns:

  • (Object) —

    the value returned by the block

Raises:

  • (Git::Error) —

    if the block raises a SystemCallError outside an unguarded region



68
69
70
71
72
73
74
# File 'lib/git/system_call_guard.rb', line 68

def call(message)
  yield self
rescue SystemCallError => e
  raise if e.equal?(@unguarded_error)

  raise Git::Error, "#{message}: #{e.message}"
end

#unguarded { ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run the block with SystemCallError conversion suspended

If the block raises a SystemCallError, the enclosing #call re-raises that same error unchanged. Any other SystemCallError reaching #call, including one raised while unwinding from the block's error, is converted.

Yields:

  • the region exempt from conversion

Yield Returns:

  • (Object) —

    returned as the method's return value

Returns:

  • (Object) —

    the value returned by the block



88
89
90
91
92
93
# File 'lib/git/system_call_guard.rb', line 88

def unguarded
  yield
rescue SystemCallError => e
  @unguarded_error = e
  raise
end