Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WindowsFile: Show the path when Win32Error is occured #3329

Merged
merged 1 commit into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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