Skip to content

Add a expected_failure utility#77

Open
vchuravy wants to merge 4 commits into
JuliaPluto:mainfrom
vchuravy:vc/expected_failure
Open

Add a expected_failure utility#77
vchuravy wants to merge 4 commits into
JuliaPluto:mainfrom
vchuravy:vc/expected_failure

Conversation

@vchuravy

@vchuravy vchuravy commented May 5, 2026

Copy link
Copy Markdown
Collaborator

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.

@adrhill

adrhill commented May 5, 2026

Copy link
Copy Markdown
Collaborator

This would be very useful our course as well (CC @alevas).

Is there a way to add some custom CSS for these @fonsp? We already use quite a lot of admonition boxes.

@eford

eford commented May 5, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the suggestion.
FWIW, I've been using PlutoTests.@test_throws for something similar... students get a green light if it fails as it should and red light if it runs when it shouldn't.

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?

  • What about using a danger admonition for if it passes when it shouldn't, but no admonition if it fails as intended?
  • If that sounds good, then I'd suggest following patterns from computational_thinking.jl. That would also help with maintaining multi-lingual support.

@vchuravy

vchuravy commented May 5, 2026

Copy link
Copy Markdown
Collaborator Author

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

@eford

eford commented May 5, 2026

Copy link
Copy Markdown
Collaborator

Something like:

try
    1 / "a"
catch e
    Markdown.parse("Error: $(sprint(showerror, e))")
end

?

@vchuravy

vchuravy commented May 5, 2026

Copy link
Copy Markdown
Collaborator Author

Not quite...

I was looking for how Pluto actually renders

error("Broken")

The closest I gotten sofar is:

try
    1 / "a"
catch e
	@htl("""
<div>
<jlerror>
<header>
$(Markdown.parse("""
$(sprint(showerror, e))
"""))
</header>
</jlerror>
</div>
""")
end

but that is also not quite it.

@vchuravy

vchuravy commented May 5, 2026

Copy link
Copy Markdown
Collaborator Author

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.

@fonsp

fonsp commented May 6, 2026

Copy link
Copy Markdown
Member

Nice! I searched a bit how you can use Pluto's built-in ANSI coloring, and it turns out that Text (or anything that outputs MIME"text/plain") is colored. So you can use embed_display to put it inside you own HTML.

And using <jlerror><header>... gives a similar visual style to Pluto's error display by mimicking its HTML structure.

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
end

I would add a bit of text to this snippet to make it clear that this is an expected error.

@vchuravy

vchuravy commented May 6, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks @fonsp!

It now looks like this:

image

@adrhill

adrhill commented May 6, 2026

Copy link
Copy Markdown
Collaborator

As a side note, it would be great to have i18n support for these (maybe in a follow-up PR).

@vchuravy

vchuravy commented May 6, 2026

Copy link
Copy Markdown
Collaborator Author

How would that look like? I am just staring at the html that Pluto produces nativly

@fonsp

fonsp commented May 6, 2026

Copy link
Copy Markdown
Member

I added i18n with 🤖

@fonsp

fonsp commented May 6, 2026

Copy link
Copy Markdown
Member

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants