Skip to content

Commit b9386f2

Browse files
author
Jack Dunham
committed
Improve error handling of empty iterators
1 parent 481089a commit b9386f2

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/iterators.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ mutable struct RegionIterator{Problem, RegionPlan} <: AbstractNetworkIterator
4747
which_region::Int
4848
const which_sweep::Int
4949
function RegionIterator(problem::P, region_plan::R, sweep::Int) where {P, R}
50-
if length(region_plan) == 0
51-
throw(BoundsError("Cannot construct a region iterator with 0 elements."))
50+
try
51+
first(region_plan)
52+
catch e
53+
if e isa BoundsError
54+
throw(ArgumentError("Cannot construct a region iterator with 0 elements."))
55+
end
56+
rethrow()
5257
end
5358
return new{P, R}(problem, region_plan, 1, sweep)
5459
end
@@ -125,10 +130,11 @@ mutable struct SweepIterator{Problem, Iter} <: AbstractNetworkIterator
125130
which_sweep::Int
126131
function SweepIterator(problem::Prob, sweep_kwargs::Iter) where {Prob, Iter}
127132
stateful_sweep_kwargs = Iterators.Stateful(sweep_kwargs)
133+
128134
first_state = Iterators.peel(stateful_sweep_kwargs)
129135

130136
if isnothing(first_state)
131-
throw(BoundsError("Cannot construct a sweep iterator with 0 elements."))
137+
throw(ArgumentError("Cannot construct a sweep iterator with 0 elements."))
132138
end
133139

134140
first_kwargs, _ = first_state

test/test_iterators.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ end
6565
@test only(cb) == 1
6666

6767
prob = TestIteratorUtils.TestProblem([])
68-
@test_throws BoundsError SweepIterator(prob, 0)
69-
@test_throws BoundsError RegionIterator(prob, [], 1)
68+
@test_throws ArgumentError SweepIterator(prob, 0)
69+
@test_throws ArgumentError RegionIterator(prob, [], 1)
7070
end
7171

7272
TI = TestIteratorUtils.TestIterator(1, 4, [])

0 commit comments

Comments
 (0)