diff --git a/NEWS.md b/NEWS.md index 2f1f350..1c348d7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 ------- diff --git a/src/Visualize.jl b/src/Visualize.jl index 5800e87..15bfce0 100644 --- a/src/Visualize.jl +++ b/src/Visualize.jl @@ -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