Skip to content

Commit 7376d76

Browse files
committed
Refactor errors that are raised by this gem
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 7e99b17 commit 7376d76

18 files changed

+328
-321
lines changed

README.md

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -104,56 +104,22 @@ Pass the `--all` option to `git log` as follows:
104104

105105
## Errors Raised By This Gem
106106

107-
This gem raises custom errors that derive from `Git::Error`. These errors are
108-
arranged in the following class heirarchy:
107+
The git gem will only raise an `ArgumentError` or an error that is a subclass of
108+
`Git::Error`. It does not explicitly raise any other types of errors.
109109

110-
Error heirarchy:
111-
112-
```text
113-
Error
114-
└── CommandLineError
115-
├── FailedError
116-
└── SignaledError
117-
└── TimeoutError
118-
```
119-
120-
Other standard errors may also be raised like `ArgumentError`. Each method should
121-
document the errors it may raise.
122-
123-
Description of each Error class:
124-
125-
* `Error`: This catch-all error serves as the base class for other custom errors in this
126-
gem. Errors of this class are raised when no more approriate specific error to
127-
raise.
128-
* `CommandLineError`: This error is raised when there's a problem executing the git
129-
command line. This gem will raise a more specific error depending on how the
130-
command line failed.
131-
* `FailedError`: This error is raised when the git command line exits with a non-zero
132-
status code that is not expected by the git gem.
133-
* `SignaledError`: This error is raised when the git command line is terminated as a
134-
result of receiving a signal. This could happen if the process is forcibly
135-
terminated or if there is a serious system error.
136-
* `TimeoutError`: This is a specific type of `SignaledError` that is raised when the
137-
git command line operation times out and is killed via the SIGKILL signal. This
138-
happens if the operation takes longer than the timeout duration configured in
139-
`Git.config.timeout` or via the `:timeout` parameter given in git methods that
140-
support this parameter.
141-
142-
`Git::GitExecuteError` remains as an alias for `Git::Error`. It is considered
143-
deprecated as of git-2.0.0.
144-
145-
Here is an example of catching errors when using the git gem:
110+
It is recommended to rescue `Git::Error` to catch any runtime error raised by
111+
this gem unless you need more specific error handling.
146112

147113
```ruby
148114
begin
149-
timeout_duration = 0.001 # seconds
150-
repo = Git.clone('https://github.com/ruby-git/ruby-git', 'ruby-git-temp', timeout: timeout_duration)
151-
rescue Git::TimeoutError => e # Catch the more specific error first!
152-
puts "Git clone took too long and timed out #{e}"
115+
# some git operation
153116
rescue Git::Error => e
154-
puts "Received the following error: #{e}"
117+
puts "An error occurred: #{e.message}"
118+
end
155119
```
156120

121+
See [`Git::Error`](https://rubydoc.info/gems/git/Git/Error) for more information.
122+
157123
## Specifying And Handling Timeouts
158124

159125
The timeout feature was added in git gem version `2.0.0`.

git.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
2727
s.required_ruby_version = '>= 3.0.0'
2828
s.requirements = ['git 2.28.0 or greater']
2929

30+
s.add_runtime_dependency 'activesupport', '>= 5.0'
3031
s.add_runtime_dependency 'addressable', '~> 2.8'
3132
s.add_runtime_dependency 'process_executer', '~> 1.1'
3233
s.add_runtime_dependency 'rchardet', '~> 1.8'

lib/git.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
1-
# Add the directory containing this file to the start of the load path if it
2-
# isn't there already.
3-
$:.unshift(File.dirname(__FILE__)) unless
4-
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
1+
require 'active_support'
2+
require 'active_support/deprecation'
3+
4+
module Git
5+
Deprecation = ActiveSupport::Deprecation.new('3.0', 'Git')
6+
end
57

68
require 'git/author'
79
require 'git/base'
810
require 'git/branch'
911
require 'git/branches'
10-
require 'git/command_line_error'
1112
require 'git/command_line_result'
1213
require 'git/command_line'
1314
require 'git/config'
1415
require 'git/diff'
1516
require 'git/encoding_utils'
16-
require 'git/error'
17+
require 'git/errors'
1718
require 'git/escaped_path'
18-
require 'git/failed_error'
19-
require 'git/git_execute_error'
2019
require 'git/index'
2120
require 'git/lib'
2221
require 'git/log'
2322
require 'git/object'
2423
require 'git/path'
2524
require 'git/remote'
2625
require 'git/repository'
27-
require 'git/signaled_error'
2826
require 'git/status'
2927
require 'git/stash'
3028
require 'git/stashes'
31-
require 'git/timeout_error'
3229
require 'git/url'
3330
require 'git/version'
3431
require 'git/working_directory'

lib/git/command_line.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
require 'git/base'
44
require 'git/command_line_result'
5-
require 'git/failed_error'
6-
require 'git/signaled_error'
5+
require 'git/errors'
76
require 'stringio'
87

98
module Git
@@ -186,7 +185,7 @@ def initialize(env, binary_path, global_opts, logger)
186185
#
187186
# @raise [Git::FailedError] if the command returned a non-zero exitstatus
188187
#
189-
# @raise [Git::GitExecuteError] if an exception was raised while collecting subprocess output
188+
# @raise [Git::ProcessIOError] if an exception was raised while collecting subprocess output
190189
#
191190
# @raise [Git::TimeoutError] if the command times out
192191
#
@@ -260,14 +259,14 @@ def post_process_all(writers, normalize, chomp)
260259
# @param pipe_name [Symbol] the name of the pipe that raised the exception
261260
# @param pipe [ProcessExecuter::MonitoredPipe] the pipe that raised the exception
262261
#
263-
# @raise [Git::GitExecuteError]
262+
# @raise [Git::ProcessIOError]
264263
#
265264
# @return [void] this method always raises an error
266265
#
267266
# @api private
268267
#
269268
def raise_pipe_error(git_cmd, pipe_name, pipe)
270-
raise Git::GitExecuteError.new("Pipe Exception for #{git_cmd}: #{pipe_name}"), cause: pipe.exception
269+
raise Git::ProcessIOError.new("Pipe Exception for #{git_cmd}: #{pipe_name}"), cause: pipe.exception
271270
end
272271

273272
# Execute the git command and collect the output
@@ -281,7 +280,7 @@ def raise_pipe_error(git_cmd, pipe_name, pipe)
281280
#
282281
# If the command does not respond to SIGKILL, it will hang this method.
283282
#
284-
# @raise [Git::GitExecuteError] if an exception was raised while collecting subprocess output
283+
# @raise [Git::ProcessIOError] if an exception was raised while collecting subprocess output
285284
# @raise [Git::TimeoutError] if the command times out
286285
#
287286
# @return [ProcessExecuter::Status] the status of the completed subprocess
@@ -334,6 +333,8 @@ def writers(out, err)
334333
#
335334
# @raise [Git::FailedError] if the command failed
336335
# @raise [Git::SignaledError] if the command was signaled
336+
# @raise [Git::TimeoutError] if the command times out
337+
# @raise [Git::ProcessIOError] if an exception was raised while collecting subprocess output
337338
#
338339
# @api private
339340
#
@@ -361,7 +362,7 @@ def process_result(git_cmd, status, out, err, normalize, chomp, timeout)
361362
#
362363
# If the command does not respond to SIGKILL, it will hang this method.
363364
#
364-
# @raise [Git::GitExecuteError] if an exception was raised while collecting subprocess output
365+
# @raise [Git::ProcessIOError] if an exception was raised while collecting subprocess output
365366
# @raise [Git::TimeoutError] if the command times out
366367
#
367368
# @return [Git::CommandLineResult] the result of the command to return to the caller

lib/git/command_line_error.rb

Lines changed: 0 additions & 59 deletions
This file was deleted.

lib/git/error.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)