diff --git a/src/Bridges/Variable/map.jl b/src/Bridges/Variable/map.jl index cfe16733b3..eca4290a41 100644 --- a/src/Bridges/Variable/map.jl +++ b/src/Bridges/Variable/map.jl @@ -579,11 +579,13 @@ Throw an error if some bridged variables do not have any reverse mapping. """ function throw_if_cannot_unbridge(map::Map) if map.unbridged_function === nothing - error( - "Cannot unbridge function because some variables are bridged by", - " variable bridges that do not support reverse mapping, for example,", - " `ZerosBridge`.", + err = MOI.GetAttributeNotAllowed( + MOI.ConstraintFunction(), + "Cannot unbridge function because some variables are bridged by " * + "variable bridges that do not support reverse mapping, for " * + "example, `ZerosBridge`.", ) + throw(err) end end diff --git a/test/Bridges/Variable/map.jl b/test/Bridges/Variable/map.jl index d519273ad0..932b01d912 100644 --- a/test/Bridges/Variable/map.jl +++ b/test/Bridges/Variable/map.jl @@ -54,7 +54,8 @@ function test_map() S1 = typeof(set1) v1, c1 = MOI.Bridges.Variable.add_key_for_bridge(map, () -> b1, set1) MOI.is_valid(map, c1) - cannot_unbridge_err = ErrorException( + cannot_unbridge_err = MOI.GetAttributeNotAllowed( + MOI.ConstraintFunction(), "Cannot unbridge function because some variables are bridged by variable" * " bridges that do not support reverse mapping, for example, `ZerosBridge`.", ) diff --git a/test/Bridges/Variable/zeros.jl b/test/Bridges/Variable/zeros.jl index 73cf9d779a..923bc868b0 100644 --- a/test/Bridges/Variable/zeros.jl +++ b/test/Bridges/Variable/zeros.jl @@ -84,14 +84,15 @@ function test_zeros() ) @test_throws err MOI.delete(bridged_mock, cyz) - err = ErrorException( + err = MOI.GetAttributeNotAllowed( + MOI.ConstraintFunction(), "Cannot unbridge function because some variables are bridged by" * " variable bridges that do not support reverse mapping, for example," * " `ZerosBridge`.", ) @test_throws err MOI.get(bridged_mock, MOI.ObjectiveFunction{typeof(obj)}()) # With `c1`, the function does not contain any variable so it tests that it - # also throws an error even if it never calls `variable_unbridged_function`. + # also throws an error even if it never calls `variable_unbridged_function` @test_throws err MOI.get(bridged_mock, MOI.ConstraintFunction(), c1) @test_throws err MOI.get(bridged_mock, MOI.ConstraintFunction(), c2) diff --git a/test/Bridges/bridge_optimizer.jl b/test/Bridges/bridge_optimizer.jl index 9493f4c0e4..97fe163f03 100644 --- a/test/Bridges/bridge_optimizer.jl +++ b/test/Bridges/bridge_optimizer.jl @@ -1176,6 +1176,17 @@ function test_ListOfVariablesWithAttributeSet(T = Float64) return end +function test_cannot_unbridge_variable_function() + model = MOI.Bridges.Variable.Zeros{Float64}(MOI.Utilities.Model{Float64}()) + x, c = MOI.add_constrained_variables(model, MOI.Zeros(1)) + c2 = MOI.add_constraint(model, 1.0 * x[1], MOI.EqualTo(1.0)) + @test_throws( + MOI.GetAttributeNotAllowed{MOI.ConstraintFunction}, + MOI.get(model, MOI.ConstraintFunction(), c2), + ) + return +end + end # module TestBridgeOptimizer.runtests()