Skip to content

Commit 083be27

Browse files
authored
Improve UnsupportedConstraint error (#2530)
1 parent a15b67f commit 083be27

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

src/constraints.jl

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,47 @@ function supports_constraint(
2626
end
2727

2828
"""
29-
struct UnsupportedConstraint{F<:AbstractFunction, S<:AbstractSet} <: UnsupportedError
30-
message::String # Human-friendly explanation why the attribute cannot be set
29+
struct UnsupportedConstraint{F<:AbstractFunction,S<:AbstractSet} <: UnsupportedError
30+
message::String
3131
end
3232
3333
An error indicating that constraints of type `F`-in-`S` are not supported by
3434
the model, that is, that [`supports_constraint`](@ref) returns `false`.
35+
36+
```jldoctest
37+
julia> import MathOptInterface as MOI
38+
39+
julia> showerror(stdout, MOI.UnsupportedConstraint{MOI.VariableIndex,MOI.ZeroOne}())
40+
UnsupportedConstraint: `MathOptInterface.VariableIndex`-in-`MathOptInterface.ZeroOne` constraints are not supported by the
41+
solver you have chosen, and we could not reformulate your model into a
42+
form that is supported.
43+
44+
To fix this error you must choose a different solver.
45+
46+
```
3547
"""
3648
struct UnsupportedConstraint{F<:AbstractFunction,S<:AbstractSet} <:
3749
UnsupportedError
38-
message::String # Human-friendly explanation why the attribute cannot be set
50+
# Human-friendly explanation why the attribute cannot be set
51+
message::String
3952
end
53+
4054
UnsupportedConstraint{F,S}() where {F,S} = UnsupportedConstraint{F,S}("")
4155

42-
function element_name(::UnsupportedConstraint{F,S}) where {F,S}
43-
return "`$F`-in-`$S` constraint"
56+
function Base.showerror(io::IO, err::UnsupportedConstraint{F,S}) where {F,S}
57+
print(
58+
io,
59+
"""
60+
UnsupportedConstraint: `$F`-in-`$S` constraints are not supported by the
61+
solver you have chosen, and we could not reformulate your model into a
62+
form that is supported.
63+
64+
To fix this error you must choose a different solver.
65+
66+
$(err.message)
67+
""",
68+
)
69+
return
4470
end
4571

4672
"""

test/errors.jl

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,30 @@ function test_errors_inconsistent_vectorscalar()
4646
return
4747
end
4848

49-
function _test_errors_UnsupportedConstraint(f)
49+
function test_errors_UnsupportedConstraint()
5050
model = DummyModel()
5151
vi = MOI.VariableIndex(1)
52-
@test_throws(MOI.UnsupportedConstraint, f(model, vi, MOI.EqualTo(0)),)
52+
@test_throws(
53+
MOI.UnsupportedConstraint,
54+
MOI.add_constraint(model, vi, MOI.EqualTo(0)),
55+
)
56+
msg = """
57+
UnsupportedConstraint: `$(MOI.VariableIndex)`-in-`$(MOI.EqualTo{Int})` constraints are not supported by the
58+
solver you have chosen, and we could not reformulate your model into a
59+
form that is supported.
60+
61+
To fix this error you must choose a different solver.
62+
63+
64+
"""
5365
try
54-
f(model, vi, MOI.EqualTo(0))
66+
MOI.add_constraint(model, vi, MOI.EqualTo(0))
5567
catch err
56-
@test sprint(showerror, err) ==
57-
"$(MOI.UnsupportedConstraint{MOI.VariableIndex,MOI.EqualTo{Int}}):" *
58-
" `$MOI.VariableIndex`-in-`$MOI.EqualTo{$Int}` constraint is" *
59-
" not supported by the model."
68+
@test sprint(showerror, err) == msg
6069
end
6170
return
6271
end
6372

64-
function test_errors_UnsupportedConstraint()
65-
_test_errors_UnsupportedConstraint(MOI.add_constraint)
66-
return
67-
end
68-
6973
function test_errors_UnsupportedConstraint_shortcut()
7074
model = DummyModel()
7175
vi = MOI.VariableIndex(1)

0 commit comments

Comments
 (0)