shard-spectator/src/spectator/error_result.cr
2021-01-30 20:07:36 -07:00

47 lines
1.3 KiB
Crystal

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
# Calls the `error` method on *visitor*.
def accept(visitor)
visitor.error(yield self)
end
# One-word description of the result.
def to_s(io)
io << "error"
end
# 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
end
end
end