Skip to content

Commit

Permalink
Add error hints for package extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-kev committed Dec 10, 2024
1 parent e4494a8 commit 09e360d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ However, functions like `resampled_as` and interpolating using a `OutputVar` wil
as an interpolant must be generated. This means repeated calls to these functions will be
slower compared to the previous versions of ClimaAnalysis.

## Better error messages
There is now error hints when using a function that requires another package such as Makie
or GeoMakie to be loaded as well. The error hint tells the user which package need to be
loaded in, so that the function can be used.

v0.5.12
-------

Expand Down
54 changes: 54 additions & 0 deletions src/Visualize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,58 @@ function plot_boxplot! end

function plot_leaderboard! end

extension_fns = [
:Makie => [
:heatmap2D!,
:sliced_heatmap!,
:heatmap!,
:line_plot1D!,
:sliced_line_plot!,
:line_plot!,
:sliced_plot!,
:plot!,
:plot_boxplot!,
:plot_leaderboard!,
:_constrained_cmap,
],
:GeoMakie => [
:oceanmask,
:landmask,
:heatmap2D_on_globe!,
:contour2D_on_globe!,
:plot_bias_on_globe!,
],
]

"""
is_pkg_loaded(pkg::Symbol)
Check if `pkg` is loaded or not.
"""
function is_pkg_loaded(pkg::Symbol)
return any(k -> Symbol(k.name) == pkg, keys(Base.loaded_modules))
end

function __init__()
# Register error hint if a package is not loaded
if isdefined(Base.Experimental, :register_error_hint)
Base.Experimental.register_error_hint(
MethodError,
) do io, exc, _argtypes, _kwargs
for (pkg, fns) in extension_fns
if Symbol(exc.f) in fns && !is_pkg_loaded(pkg)
if pkg == :Makie
print(
io,
"\nImport one of the Makie backends (GLMakie, CairoMakie, WGLMakie, RPRMakie, etc.) to enable `$(exc.f)`.";
)
elseif pkg == :GeoMakie
print(io, "\nImport GeoMakie to enable `$(exc.f)`.";)
end
end
end
end
end
end

end

0 comments on commit 09e360d

Please sign in to comment.