What is the best future-proof way to check which function raised a MethodError?

I want to ensure that the MethodError is raised by by a function in my package, and not downstream.

For example here’s one way:

julia> e = MethodError(sin, "a")
MethodError(sin, "a", 0xffffffffffffffff)

julia> e.f == sin
true

but I’m not sure if this is too internal to be used in a package. Another way is to capture the error string and process it to extract the function, eg something along these lines:

julia> s = try
           sin("a")
       catch e
           io = IOBuffer()
           showerror(io, e)
           s = String(take!(io))
           split(s, '\n')[1]
       end
"MethodError: no method matching sin(::String)"

julia> startswith(s, "MethodError: no method matching sin")
true

This seems clunky and might break if the error string is changed at any point.

Is there a better way to achieve this?

AFAIK errors have no public API other than the type hierarchy.

It is not clear to me why/how you would want to

I would recommend just aiming to get rid of all errors in your package which are bugs, and just pass through the rest. Generally you don’t want to use errors for control flow.