Handling errors when overwriting Base.show


#1

Hello,
How does one handle errors thrown in a Base.show overwrite so that they’re displayed in the REPL and not like this:

this is generated from a file with

struct Foo
    fn::String
end

function Base.show(io::IO, ::MIME"image/png", f::Foo)
    isfile(f.fn) || error("File not found")
    write(io, read(f.fn))
end

Foo("mario.png") # works as expected
Foo("mario_notfound.png") # leads to the error above, I'd like that error to be in the REPL

Interestingly if I copy paste the exact same code in the REPL itself, the error shows in the REPL (though the plot pane still opens which I’d like to avoid).

So to summarise:

  • Is there a proper way to collect & display errors that may arise in the application of a Base.show so that they are shown in the REPL and not “on the side”
  • Is there a way to not trigger the opening of the plot-pane if the Base.show fails?

thanks!

PS: if it seems weird to have possible errors in a Base.show and not earlier, the reason is that I’d like Foo to be a container for information to be passed to an external program which then generates the PNG, I want this to only be triggered when necessary and so not a-priori which is why the run command would happen in the Base.show. But since it may fail, I’d like to know how to do proper error handling.


#2

Not for you, no. This needs to be addressed in Juno itself. Feel free to open an issue at the Juno.jl repo and I’ll look into it.

No, we need to open the plot pane before a show method is called to get the panes size and pass that to show. Arguably we should only do that for application/prs.juno.plotpane+html and not for more standard mime types, but e.g. Plots.jl’s Juno integration expects that information for normal mime types as well.


#3

Ok, very clear. I’ll open an issue for the first one. Thanks Sebastian!