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 5, 2024
1 parent e4494a8 commit 3956c15
Show file tree
Hide file tree
Showing 2 changed files with 55 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
50 changes: 50 additions & 0 deletions src/Visualize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,54 @@ 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))

Check warning on line 66 in src/Visualize.jl

View check run for this annotation

Codecov / codecov/patch

src/Visualize.jl#L65-L66

Added lines #L65 - L66 were not covered by tests
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)
print(
io,
"\nImport $pkg to enable `$(exc.f)`. You might also need to import Makie and its backends (CairoMakie, GLMake, etc.) as well.";
)
end
end
end
end
end

end

0 comments on commit 3956c15

Please sign in to comment.