Skip to content

Commit

Permalink
Allow non-exception objects to be passed to exception: keyword argu…
Browse files Browse the repository at this point in the history
…ment. Fixes #66.
  • Loading branch information
ioquatix committed Dec 9, 2024
1 parent ebd30ec commit 4638350
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/console/output/failure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ def call(subject = nil, *arguments, exception: nil, **options, &block)
if last.is_a?(Exception)
options[:event] = Event::Failure.for(last)
end
else
elsif exception.is_a?(Exception)
options[:event] = Event::Failure.for(exception)
else
# We don't know what this is, so we just pass it through:
options[:exception] = exception
end

super(subject, *arguments, **options)
Expand Down
43 changes: 43 additions & 0 deletions test/console/output/failure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2021-2024, by Samuel Williams.

require "console/logger"
require "console/capture"

require "console/output/failure"

describe Console::Output::Failure do
let(:output) {Console::Capture.new}
let(:logger) {subject.new(output)}
let(:error) {StandardError.new("Something went wrong")}

it "logs exception messages" do
logger.call(self, error)

expect(output.last).to have_keys(
event: have_keys(
message: be == "Something went wrong"
)
)
end

it "logs exception keyword argument" do
logger.call(self, exception: error)

expect(output.last).to have_keys(
event: have_keys(
message: be == "Something went wrong"
)
)
end

it "logs non-exception exception keyword argument" do
logger.call(self, exception: "Something went wrong")

expect(output.last).to have_keys(
exception: be == "Something went wrong"
)
end
end

0 comments on commit 4638350

Please sign in to comment.