Skip to content

Commit

Permalink
Merge pull request #227 from control-toolbox/224-only-give-weakdeps
Browse files Browse the repository at this point in the history
update ExtensionError
  • Loading branch information
ocots authored Aug 7, 2024
2 parents e14eaf8 + 4e478a6 commit 5bf3b96
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
22 changes: 19 additions & 3 deletions src/exception.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,29 @@ Exception thrown when an extension is not loaded but the user tries to call a fu
$(TYPEDFIELDS)
"""
struct ExtensionError <: CTException
var::String
mutable struct ExtensionError <: CTException
weakdeps::Tuple{Vararg{Symbol}}
function ExtensionError(weakdeps::Symbol...)
isempty(weakdeps) && throw(UnauthorizedCall("Please provide at least one weak dependence for the extension."))
e = new()
e.weakdeps = weakdeps
return e
end
end

"""
$(TYPEDSIGNATURES)
Print the exception.
"""
Base.showerror(io::IO, e::ExtensionError) = print(io, "ExtensionError: ", e.var)
function Base.showerror(io::IO, e::ExtensionError)
print(io, "ExtensionError. Please make: ")
printstyled(io, "julia>", color=:green, bold=true)
printstyled(io, " using ", color=:magenta)
N = size(e.weakdeps, 1)
for i range(1, N)
wd = e.weakdeps[i]
i < N ? print(io, string(wd), ", ") : print(io, string(wd))
end
nothing
end
2 changes: 1 addition & 1 deletion test/test_exception.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ e = NotImplemented("blabla")
@test_throws ErrorException error(e)
@test typeof(sprint(showerror, e)) == String

e = ExtensionError("tatat")
e = ExtensionError(:tata)
@test_throws ErrorException error(e)
@test typeof(sprint(showerror, e)) == String

Expand Down

0 comments on commit 5bf3b96

Please sign in to comment.