shard-spectator/src/spectator/error_result.cr

48 lines
1.3 KiB
Crystal
Raw Normal View History

2020-10-17 20:56:31 +00:00
require "./fail_result"
module Spectator
# Outcome that indicates running an example generated an error.
# This occurs when an unexpected exception was raised while running an example.
# This is different from a "failed" result in that the error was not from a failed assertion.
class ErrorResult < FailResult
# Calls the `error` method on *visitor*.
def accept(visitor)
visitor.error
end
2021-01-31 02:42:46 +00:00
# Calls the `error` method on *visitor*.
def accept(visitor)
visitor.error(yield self)
end
2020-10-17 20:56:31 +00:00
# One-word description of the result.
def to_s(io)
io << "error"
end
2021-01-31 02:42:46 +00:00
2021-01-31 03:07:36 +00:00
# Adds the common JSON fields for all result types
# and fields specific to errored results.
private def add_json_fields(json : ::JSON::Builder)
super
json.field("exceptions") do
exception = error
json.array do
while exception
error_to_json(exception, json) if exception
exception = error.cause
end
end
end
end
# Adds a single exception to a JSON builder.
private def error_to_json(error : Exception, json : ::JSON::Builder)
json.object do
json.field("type", error.class.to_s)
json.field("message", error.message)
json.field("backtrace", error.backtrace)
end
2021-01-31 02:42:46 +00:00
end
2020-10-17 20:56:31 +00:00
end
end