Skip to content

Commit 06c76a9

Browse files
committed
Implement and test a custom exception type for unknown actions in LSM
1 parent 0900db2 commit 06c76a9

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/utils/LoopStateMachine/LoopStateMachine.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ function Base.show(io::IO, action::Action{T}) where T
6565
end
6666
end
6767

68+
struct UnrecognizedActionException <: Base.Exception
69+
name::Symbol
70+
end
71+
72+
function Base.showerror(io::IO, e::UnrecognizedActionException)
73+
print(io, "Unrecognized action: ")
74+
printstyled(io, e.name; color = :red, bold = true)
75+
println(io, ".")
76+
println(io, "Valid actions are:")
77+
println(io, ALL_ACTION_DESCRIPTIONS)
78+
end
79+
80+
# We exclude the macro definition from code coverage computations,
81+
# because I know it's tested but Codecov doesn't seem to think so.
82+
# COV_EXCL_START
6883
"""
6984
@controlflow f(...)
7085
@@ -94,13 +109,14 @@ macro controlflow(expr)
94109
elseif $varname.name == :full_return
95110
return $varname
96111
else
97-
throw(ArgumentError("Unknown action: $($varname.name)"))
112+
throw(UnrecognizedActionException($varname.name))
98113
end
99114
else
100115
$varname
101116
end
102117
end
103118
end
119+
# COV_EXCL_STOP
104120

105121
# You can define more actions as you desire.
106122

test/utils/LoopStateMachine.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Test
2+
import GeometryOps.LoopStateMachine
23
using GeometryOps.LoopStateMachine: @controlflow, Action
34

45
@testset "Continue action" begin
@@ -73,3 +74,17 @@ end
7374
@test sprint(print, Action(:x)) == "Action(:x)"
7475
end
7576

77+
@testset "Unknown action" begin
78+
f(i) = Action(:unknown_action, i)
79+
@test_throws LoopStateMachine.UnrecognizedActionException begin
80+
for i in 1:3
81+
@controlflow f(i)
82+
end
83+
end
84+
85+
@test_throws ":continue" begin
86+
for i in 1:3
87+
@controlflow f(i)
88+
end
89+
end
90+
end

0 commit comments

Comments
 (0)