diff --git a/src/constraints.jl b/src/constraints.jl index 9cee1cbcfa..03550dca77 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -26,21 +26,47 @@ function supports_constraint( end """ - struct UnsupportedConstraint{F<:AbstractFunction, S<:AbstractSet} <: UnsupportedError - message::String # Human-friendly explanation why the attribute cannot be set + struct UnsupportedConstraint{F<:AbstractFunction,S<:AbstractSet} <: UnsupportedError + message::String end An error indicating that constraints of type `F`-in-`S` are not supported by the model, that is, that [`supports_constraint`](@ref) returns `false`. + +```jldoctest +julia> import MathOptInterface as MOI + +julia> showerror(stdout, MOI.UnsupportedConstraint{MOI.VariableIndex,MOI.ZeroOne}()) +UnsupportedConstraint: `MathOptInterface.VariableIndex`-in-`MathOptInterface.ZeroOne` constraints are not supported by the +solver you have chosen, and we could not reformulate your model into a +form that is supported. + +To fix this error you must choose a different solver. + +``` """ struct UnsupportedConstraint{F<:AbstractFunction,S<:AbstractSet} <: UnsupportedError - message::String # Human-friendly explanation why the attribute cannot be set + # Human-friendly explanation why the attribute cannot be set + message::String end + UnsupportedConstraint{F,S}() where {F,S} = UnsupportedConstraint{F,S}("") -function element_name(::UnsupportedConstraint{F,S}) where {F,S} - return "`$F`-in-`$S` constraint" +function Base.showerror(io::IO, err::UnsupportedConstraint{F,S}) where {F,S} + print( + io, + """ + UnsupportedConstraint: `$F`-in-`$S` constraints are not supported by the + solver you have chosen, and we could not reformulate your model into a + form that is supported. + + To fix this error you must choose a different solver. + + $(err.message) + """, + ) + return end """ diff --git a/test/errors.jl b/test/errors.jl index 459e010eb2..baeedd87b1 100644 --- a/test/errors.jl +++ b/test/errors.jl @@ -46,26 +46,30 @@ function test_errors_inconsistent_vectorscalar() return end -function _test_errors_UnsupportedConstraint(f) +function test_errors_UnsupportedConstraint() model = DummyModel() vi = MOI.VariableIndex(1) - @test_throws(MOI.UnsupportedConstraint, f(model, vi, MOI.EqualTo(0)),) + @test_throws( + MOI.UnsupportedConstraint, + MOI.add_constraint(model, vi, MOI.EqualTo(0)), + ) + msg = """ + UnsupportedConstraint: `$(MOI.VariableIndex)`-in-`$(MOI.EqualTo{Int})` constraints are not supported by the + solver you have chosen, and we could not reformulate your model into a + form that is supported. + + To fix this error you must choose a different solver. + + + """ try - f(model, vi, MOI.EqualTo(0)) + MOI.add_constraint(model, vi, MOI.EqualTo(0)) catch err - @test sprint(showerror, err) == - "$(MOI.UnsupportedConstraint{MOI.VariableIndex,MOI.EqualTo{Int}}):" * - " `$MOI.VariableIndex`-in-`$MOI.EqualTo{$Int}` constraint is" * - " not supported by the model." + @test sprint(showerror, err) == msg end return end -function test_errors_UnsupportedConstraint() - _test_errors_UnsupportedConstraint(MOI.add_constraint) - return -end - function test_errors_UnsupportedConstraint_shortcut() model = DummyModel() vi = MOI.VariableIndex(1)