diff --git a/src/Bridges/bridge_optimizer.jl b/src/Bridges/bridge_optimizer.jl index dd8afd4ff5..99335f4e46 100644 --- a/src/Bridges/bridge_optimizer.jl +++ b/src/Bridges/bridge_optimizer.jl @@ -1154,17 +1154,38 @@ function MOI.set( return end +struct ModifyBridgeNotAllowed{C<:MOI.AbstractFunctionModification} <: + MOI.NotAllowedError + change::C + message::String + + function ModifyBridgeNotAllowed( + change::MOI.AbstractFunctionModification, + msg::String = "", + ) + return new{typeof(change)}(change, msg) + end +end + +function MOI.operation_name(err::ModifyBridgeNotAllowed) + return "Modifying the bridge with $(err.change)" +end + +function MOI.modify( + ::MOI.ModelLike, + ::AbstractBridge, + change::MOI.AbstractFunctionModification, +) + return throw(ModifyBridgeNotAllowed(change)) +end + function _modify_bridged_function( b::AbstractBridgeOptimizer, ci_or_obj, change::MOI.AbstractFunctionModification, ) if is_bridged(b, ci_or_obj) - try - MOI.modify(recursive_model(b), bridge(b, ci_or_obj), change) - catch - MOI.throw_modify_not_allowed(ci_or_obj, change) - end + MOI.modify(recursive_model(b), bridge(b, ci_or_obj), change) else MOI.modify(b.model, ci_or_obj, change) end @@ -1902,11 +1923,7 @@ function MOI.modify( modify_bridged_change(b, ci, change) else if is_bridged(b, ci) - try - call_in_context(MOI.modify, b, ci, change) - catch - MOI.throw_modify_not_allowed(ci, change) - end + call_in_context(MOI.modify, b, ci, change) else MOI.modify(b.model, ci, change) end diff --git a/test/Bridges/bridge_optimizer.jl b/test/Bridges/bridge_optimizer.jl index f08e539836..27ce198de4 100644 --- a/test/Bridges/bridge_optimizer.jl +++ b/test/Bridges/bridge_optimizer.jl @@ -1047,7 +1047,10 @@ function test_modify_objective_scalar_quadratic_coefficient_change() attr = MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{T}}() MOI.set(model, attr, T(1) * x * x + T(2) * x + T(3)) change = MOI.ScalarQuadraticCoefficientChange(x, x, T(4)) - @test_throws MOI.ModifyObjectiveNotAllowed MOI.modify(model, attr, change) + @test_throws( + MOI.Bridges.ModifyBridgeNotAllowed, + MOI.modify(model, attr, change), + ) return end @@ -1071,7 +1074,18 @@ function test_modify_constraint_scalar_quadratic_coefficient_change() x = MOI.add_variable(model) c = MOI.add_constraint(model, T(1) * x * x + T(2) * x, MOI.LessThan(T(1))) change = MOI.ScalarQuadraticCoefficientChange(x, x, T(4)) - @test_throws MOI.ModifyConstraintNotAllowed MOI.modify(model, c, change) + @test_throws( + MOI.Bridges.ModifyBridgeNotAllowed, + MOI.modify(model, c, change), + ) + return +end + +function test_show_modify_bridge_not_allowed() + x = MOI.VariableIndex(1) + change = MOI.ScalarQuadraticCoefficientChange(x, x, 2.0) + err = MOI.Bridges.ModifyBridgeNotAllowed(change) + @test occursin("cannot be performed", sprint(showerror, err)) return end