Skip to content

Commit

Permalink
Win32Error: Add to_s and inspect methods and remove message
Browse files Browse the repository at this point in the history
Since the previous code doesn't have to_s method, it shows only
`Fluent::Win32Error`, doesn't show the detail.

Signed-off-by: Takuro Ashie <ashie@clear-code.com>
  • Loading branch information
ashie committed Apr 14, 2021
1 parent 927f19e commit 9a2be48
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
11 changes: 8 additions & 3 deletions lib/fluent/plugin/file_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,17 @@ def format_english_message(errcode)
buf.force_encoding(Encoding.default_external).strip
end

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

def inspect
"#<#{to_s}>"
end

def ==(other)
return false if other.class != Win32Error
@errcode == other.errcode && @msg == other.msg
Expand Down
16 changes: 13 additions & 3 deletions test/plugin/test_file_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,23 @@ def teardown

test 'ERROR_SHARING_VIOLATION message' do
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.")
"Fluent::Win32Error: 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")
"Fluent::Win32Error: code: 32, The process cannot access the file because it is being used by another process." +
" - cannot open the file")
end

test 'to_s' do
assert_equal("Fluent::Win32Error: code: 32, The process cannot access the file because it is being used by another process. - C:\file.txt",
Fluent::Win32Error.new(ERROR_SHARING_VIOLATION, "C:\file.txt").to_s)
end

test 'inspect' do
assert_equal("#<Fluent::Win32Error: code: 32, The process cannot access the file because it is being used by another process. - C:\file.txt>",
Fluent::Win32Error.new(ERROR_SHARING_VIOLATION, "C:\file.txt").inspect)
end
end

Expand Down

0 comments on commit 9a2be48

Please sign in to comment.