Skip to content

Commit 83f7d0f

Browse files
committed
Add new Extension Exceptions
1 parent 20ff589 commit 83f7d0f

1 file changed

Lines changed: 39 additions & 17 deletions

File tree

src/io.jl

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,50 @@ WriteVTK.VTKCellType(::Type{<:Quadrilateral2D}) = VTKCellTypes.VTK_QUAD
77
WriteVTK.VTKCellType(::Type{<:Tetrahedron3D}) = VTKCellTypes.VTK_TETRA
88
WriteVTK.VTKCellType(::Type{<:Hexahedron3D}) = VTKCellTypes.VTK_HEXAHEDRON
99

10-
function prepare_extension_function(fname::Symbol,x)
11-
expr = quote
12-
function $fname($x;kwargs...)
13-
@info "single parameter function"
14-
end
15-
end
16-
eval(expr)
10+
struct ExtensionLoadError <: Exception
11+
fname::Symbol
12+
weakdeps::Vector{Symbol}
13+
end
14+
15+
Base.showerror(io::IO, e::ExtensionLoadError) =
16+
print(
17+
io,
18+
"ExtensionLoadError: "
19+
* "The function " * String(e.fname) * " is part of an extension.\n"
20+
* "Suggestion: Install and load the following modules:"
21+
* mapreduce(x -> "\n" * x, *, String.(e.weakdeps))
22+
)
23+
24+
struct ExtensionMethodError <: Exception
25+
fn::Function
26+
x
27+
end
28+
29+
30+
function Base.showerror(io::IO, e::ExtensionMethodError)
31+
fname = String(nameof(e.fn))
32+
print(
33+
io,
34+
"ExtensionMethodError: no method matching " * fname * "(::" * String(nameof(typeof(e.x))) * ")\n"
35+
* "The function " * fname * " exists in a loaded extension, but no method is defined for this combination of argument types.\n"
36+
* "\n"
37+
* "Candidates are:\n"
38+
)
39+
return println.(io, filter(x -> x != which(e.fn, (typeof(e.x),)), methods(e.fn)))
1740
end
1841

19-
function prepare_extension_function(fname::Symbol,weakdeps::Vector{Symbol},x::Symbol)
42+
function prepare_extension_function(fname::Symbol, weakdeps::Vector{Symbol}, x::Symbol)
2043
expr = quote
21-
function $fname($x;kwargs...)
22-
if mapreduce(x->x in names(Main,imported=true),&,$weakdeps)
23-
@error "Extension was loaded but argument types are wrong"
44+
function $fname($x; kwargs...)
45+
if mapreduce(x -> isdefined(Main, x), &, $weakdeps)
46+
throw(ExtensionMethodError($fname, $x))
2447
else
25-
@error "This specific function is part of an extension.\n"*
26-
"To use it install and load the following modules:"*
27-
mapreduce(x->"\n"*x,*,String.($weakdeps))
48+
throw(ExtensionLoadError(nameof($fname), $weakdeps))
2849
end
50+
return nothing
2951
end
3052
end
31-
eval(expr)
53+
return eval(expr)
3254
end
3355

3456
"""
@@ -288,8 +310,8 @@ function simplexgrid(file::String, ::Type{Val{:sg}}; kwargs...)
288310
return g
289311
end
290312

291-
prepare_extension_function(:simplexgrid_from_gmsh,[:Gmsh],:filename)
292-
# function simplexgrid_from_gmsh(filename; incomplete = false, Tc = Float32, Ti = Int32)
313+
prepare_extension_function(:simplexgrid_from_gmsh, [:Gmsh], :filename)
314+
# function simplexgrid_from_gmsh(filename; incomplete = false, Tc = Float32, Ti = Int32)
293315
# throw(ErrorException("Missing Gmsh extension. Add Gmsh.jl to your environment and import it to read msh or geo files."))
294316
# end
295317

0 commit comments

Comments
 (0)