diff --git a/src/Bridges/Bridges.jl b/src/Bridges/Bridges.jl index 7c0e490c56..7678d63a1d 100644 --- a/src/Bridges/Bridges.jl +++ b/src/Bridges/Bridges.jl @@ -222,6 +222,18 @@ function _test_structural_identical( return end +_runtests_error_handler(err, ::Bool) = rethrow(err) + +function _runtests_error_handler( + err::MOI.GetAttributeNotAllowed{MOI.ConstraintFunction}, + cannot_unbridge::Bool, +) + if cannot_unbridge + return # This error is expected. Do nothing. + end + return rethrow(err) +end + """ runtests( Bridge::Type{<:AbstractBridge}, @@ -305,13 +317,8 @@ function runtests( set = try MOI.get(model, MOI.ConstraintSet(), ci) catch err - # Could be thrown by `unbridged_function` - if cannot_unbridge && - err isa MOI.GetAttributeNotAllowed{MOI.ConstraintFunction} - continue - else - rethrow(err) - end + _runtests_error_handler(err, cannot_unbridge) + continue end for attr in (MOI.ConstraintPrimalStart(), MOI.ConstraintDualStart()) if MOI.supports(model, attr, MOI.ConstraintIndex{F,S}) diff --git a/test/Bridges/Variable/zeros.jl b/test/Bridges/Variable/zeros.jl index 7dad8da30c..a531b199ef 100644 --- a/test/Bridges/Variable/zeros.jl +++ b/test/Bridges/Variable/zeros.jl @@ -46,6 +46,30 @@ function test_runtests() return end +function test_bridge_error_handler() + for (err, flag) in ( + ErrorException("abc") => false, + MOI.GetAttributeNotAllowed(MOI.ObjectiveSense()) => false, + MOI.GetAttributeNotAllowed(MOI.ConstraintFunction()) => true, + ) + @test_throws err try + @assert false + catch + MOI.Bridges._runtests_error_handler(err, false) + end + if flag + @test MOI.Bridges._runtests_error_handler(err, true) === nothing + else + @test_throws err try + @assert false + catch + MOI.Bridges._runtests_error_handler(err, true) + end + end + end + return +end + function test_zeros() mock = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}()) bridged_mock = MOI.Bridges.Variable.Zeros{Float64}(mock)