Skip to content

Commit

Permalink
Merge pull request #3329 from ashie/followup-#3325
Browse files Browse the repository at this point in the history
WindowsFile: Show the path when Win32Error is occured
  • Loading branch information
ashie authored Apr 13, 2021
2 parents 9b756ef + 927f19e commit 07df9a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
13 changes: 8 additions & 5 deletions lib/fluent/plugin/file_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ class Win32Error < StandardError
require 'windows/error'
include Windows::Error

attr_reader :errcode
attr_reader :errcode, :msg

def initialize(errcode)
def initialize(errcode, msg = nil)
@errcode = errcode
@msg = msg
end

def format_english_message(errcode)
Expand All @@ -65,12 +66,14 @@ def format_english_message(errcode)
end

def message
"code: #{@errcode}, #{format_english_message(@errcode)}"
msg = "code: #{@errcode}, #{format_english_message(@errcode)}"
msg << ": #{@msg}" if @msg
msg
end

def ==(other)
return false if other.class != Win32Error
@errcode == other.errcode
@errcode == other.errcode && @msg == other.msg
end
end

Expand Down Expand Up @@ -109,7 +112,7 @@ def initialize(path, mode='r', sharemode=FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_S
if err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND || err == ERROR_ACCESS_DENIED
raise Errno::ENOENT
end
raise Win32Error.new(err)
raise Win32Error.new(err, path)
end
end

Expand Down
17 changes: 14 additions & 3 deletions test/plugin/test_file_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ def teardown

sub_test_case 'Win32Error' do
test 'equal' do
assert_equal(Fluent::Win32Error.new(ERROR_SHARING_VIOLATION),
Fluent::Win32Error.new(ERROR_SHARING_VIOLATION))
assert_equal(Fluent::Win32Error.new(ERROR_SHARING_VIOLATION, "message"),
Fluent::Win32Error.new(ERROR_SHARING_VIOLATION, "message"))
end

test 'different error code' do
assert_not_equal(Fluent::Win32Error.new(ERROR_FILE_NOT_FOUND),
Fluent::Win32Error.new(ERROR_SHARING_VIOLATION))
end

test 'different error message' do
assert_not_equal(Fluent::Win32Error.new(ERROR_FILE_NOT_FOUND, "message1"),
Fluent::Win32Error.new(ERROR_FILE_NOT_FOUND, "message2"))
end

test 'different class' do
assert_not_equal(Errno::EPIPE,
Fluent::Win32Error.new(ERROR_SHARING_VIOLATION))
Expand All @@ -37,6 +42,12 @@ def teardown
assert_equal(Fluent::Win32Error.new(ERROR_SHARING_VIOLATION).message,
"code: 32, The process cannot access the file because it is being used by another process.")
end

test 'ERROR_SHARING_VIOLATION with a message' do
assert_equal(Fluent::Win32Error.new(ERROR_SHARING_VIOLATION, "cannot open the file").message,
"code: 32, The process cannot access the file because it is being used by another process." +
": cannot open the file")
end
end

sub_test_case 'WindowsFile exceptions' do
Expand Down Expand Up @@ -71,7 +82,7 @@ def teardown
path = "#{TMP_DIR}/test_windows_file.txt"
file1 = file2 = nil
file1 = File.open(path, "wb")
assert_raise(Fluent::Win32Error.new(ERROR_SHARING_VIOLATION)) do
assert_raise(Fluent::Win32Error.new(ERROR_SHARING_VIOLATION, path)) do
file2 = Fluent::WindowsFile.new(path, 'r', FILE_SHARE_READ)
ensure
file2.close if file2
Expand Down

0 comments on commit 07df9a0

Please sign in to comment.