Add a expected_failure utility#77
Conversation
|
Thanks for the suggestion. That said, I could still see making this into a convenience function and I like the idea of it using one or more admonition boxes. Does that sound good?
|
|
Is there any way to get Pluto actual error printer? I want the focus to be on the exception and associated message, while not emphasizing the stack trace and somehow conveying that this is intentional |
|
Something like: try
1 / "a"
catch e
Markdown.parse("Error: $(sprint(showerror, e))")
end? |
|
Not quite... I was looking for how Pluto actually renders The closest I gotten sofar is: but that is also not quite it. |
|
Trying to do things before a lecture is always a mood: using ANSIColoredPrinters
using HypertextLiteral
function expected_failure(f::Function)
try
f()
Markdown.MD(Markdown.Admonition("warning", "Expected failure", [
Markdown.Paragraph(["The code was expected to fail, but it evaluated successfully."])
]))
catch e
io = IOBuffer()
showerror(IOContext(io, :color=>true), e)
@htl("""
<div>
<link rel="stylesheet" href="https://juliadocs.org/ANSIColoredPrinters.jl/dev/assets/default.css" crossorigin=""/>
<jlerror>
$(HTMLPrinter(io))
</jlerror>
</div>
""")
end
end
is the closet I got to something looking good. |
|
Nice! I searched a bit how you can use Pluto's built-in ANSI coloring, and it turns out that And using function expected_failure_2(f::Function)
try
f()
Markdown.MD(Markdown.Admonition("warning", "Expected failure", [
Markdown.Paragraph(["The code was expected to fail, but it evaluated successfully."])
]))
catch e
str = sprint() do io
showerror(IOContext(io, :color=>true), e)
end
@htl """
<div>
<jlerror>
<header style='overflow: auto;'>
$(embed_display(Text(str)))
</header>
</jlerror>
</div>
"""
end
endI would add a bit of text to this snippet to make it clear that this is an expected error. |
|
Thanks @fonsp! It now looks like this:
|
|
As a side note, it would be great to have i18n support for these (maybe in a follow-up PR). |
|
How would that look like? I am just staring at the html that Pluto produces nativly |
|
I added i18n with 🤖 |
|
What's tricky with the i18n system in this package is that there is no fallback language. So every feature PR needs to already write i18n strings for all the languages, or the function will just show empty text in that language. That's why I used a machine translation here (for Pluto, I would leave it empty until a translator has time to look at it) |

I sometimes teach with code that is meant to fail, and I would like to make it clear that the error in the notebook is expected and not a bug.