Skip to content

Commit

Permalink
Merge pull request #113 from aozorahack/remove-exit
Browse files Browse the repository at this point in the history
remove exit in `lib/aozora2html`; use exit in `bin/aozora2html`
  • Loading branch information
takahashim authored Feb 1, 2024
2 parents 68de529 + 69eaf1b commit 3ec075f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ Layout/LineLength:
Lint/EmptyClass:
AllowComments: true

Lint/InheritException:
Enabled: false

Metrics/AbcSize:
Enabled: true
Max: 260.05
Expand Down
2 changes: 2 additions & 0 deletions bin/aozora2html
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ Dir.mktmpdir do |dir|
output = File.read(dest_file)
print output
end
rescue Aozora2Html::FatalError
exit(2)
end
2 changes: 1 addition & 1 deletion lib/aozora2html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def process
rescue Aozora2Html::Error => e
puts e.message(line_number)
if e.is_a?(Aozora2Html::Error)
exit(2)
raise Aozora2Html::FatalError
end
end
tail_output # final call
Expand Down
4 changes: 4 additions & 0 deletions lib/aozora2html/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ def message(line = 0)
I18n.t(:error_stop, line, @message)
end
end

# Aozora2Htmlクラス内でexitする代わりに例外をあげるためのfatal error用クラス
class FatalError < Exception
end
end
6 changes: 2 additions & 4 deletions lib/jstream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class Jstream
CRLF = CR + LF

# 初期化と同時に、いったん最初の行をscanして、改行コードがCR+LFかどうか調べる。
# CR+LFでない場合はエラーメッセージを出力してexitする(!)
#
# TODO: 将来的にはさすがにexitまではしないよう、仕様を変更する?
# CR+LFでない場合はエラーメッセージを出力して、例外Aozora2Html::FatalErrorを上げる
def initialize(file_io)
@line = 0
@current_char = nil
Expand All @@ -30,7 +28,7 @@ def initialize(file_io)
rescue Aozora2Html::Error => e
puts e.message(1)
if e.is_a?(Aozora2Html::Error)
exit(2)
raise Aozora2Html::FatalError
end
ensure
@file.rewind
Expand Down
2 changes: 1 addition & 1 deletion test/test_jstream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_new_error
orig_stdout = $stdout
out = StringIO.new
$stdout = out
assert_raises(SystemExit) do
assert_raises(Aozora2Html::FatalError) do
Jstream.new(strio)
end
$stdout = orig_stdout
Expand Down

0 comments on commit 3ec075f

Please sign in to comment.