Skip to content

Commit 869f2d7

Browse files
committed
Improve text of showerror for NotAllowedError
1 parent 2ecfa90 commit 869f2d7

File tree

2 files changed

+50
-41
lines changed

2 files changed

+50
-41
lines changed

src/error.jl

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,38 @@ form).
5151
function operation_name end
5252

5353
function Base.showerror(io::IO, err::NotAllowedError)
54-
print(io, typeof(err), ": ", operation_name(err), " cannot be performed")
54+
println(io, typeof(err), ":\n")
55+
println(io, "## Cause\n")
56+
print(io, operation_name(err), " cannot be performed")
5557
m = message(err)
56-
if Base.isempty(m)
57-
print(io, ".")
58-
else
59-
print(io, ": ", m)
58+
if !isempty(m)
59+
println(io, " because:\n\n", m)
6060
end
61-
return print(
61+
println(io)
62+
print(
6263
io,
63-
" You may want to use a `CachingOptimizer` in `AUTOMATIC` mode",
64-
" or you may need to call `reset_optimizer` before doing this",
65-
" operation if the `CachingOptimizer` is in `MANUAL` mode.",
64+
"""
65+
## Fixing this error
66+
67+
An `MOI.NotAllowedError` error occurs when you have tried to do something that
68+
is not implemented by the solver.
69+
70+
The most common way to fix this error is to wrap the optimizer in a
71+
`MOI.Utilities.CachingOptimizer`.
72+
73+
For example, if you are using `JuMP.Model` or `JuMP.set_optimizer`, do:
74+
```julia
75+
model = JuMP.Model(optimizer; with_cache_type = Float64)
76+
model = JuMP.GenericModel{T}(optimizer; with_cache_type = T)
77+
JuMP.set_optimizer(model, optimizer; with_cache_type = Float64)
78+
```
79+
Similarly, if you are using `MOI.instantiate`, do:
80+
```julia
81+
model = MOI.instantiate(optimizer; with_cache_type = Float64)
82+
```
83+
""",
6684
)
85+
return
6786
end
6887

6988
"""

test/General/errors.jl

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,19 @@ function test_errors_DeleteNotAllowed()
139139
try
140140
MOI.delete(model, vi)
141141
catch err
142-
@test sprint(showerror, err) ==
143-
"$(MOI.DeleteNotAllowed{typeof(vi)}): Deleting the index $vi " *
144-
"cannot be performed. You may want to use a `CachingOptimizer` " *
145-
"in `AUTOMATIC` mode or you may need to call `reset_optimizer` " *
146-
"before doing this operation if the `CachingOptimizer` is in " *
147-
"`MANUAL` mode."
142+
contents = sprint(showerror, err)
143+
@test occursin("$(MOI.DeleteNotAllowed{typeof(vi)})", contents)
144+
@test occursin("Deleting the index $vi ", contents)
145+
@test occursin("## Fixing this error", contents)
148146
end
149147
@test_throws MOI.DeleteNotAllowed{typeof(ci)} MOI.delete(model, ci)
150148
try
151149
MOI.delete(model, ci)
152150
catch err
153-
@test sprint(showerror, err) ==
154-
"$(MOI.DeleteNotAllowed{typeof(ci)}): Deleting the index $ci " *
155-
"cannot be performed. You may want to use a `CachingOptimizer` " *
156-
"in `AUTOMATIC` mode or you may need to call `reset_optimizer` " *
157-
"before doing this operation if the `CachingOptimizer` is in " *
158-
"`MANUAL` mode."
151+
contents = sprint(showerror, err)
152+
@test occursin("$(MOI.DeleteNotAllowed{typeof(ci)})", contents)
153+
@test occursin("Deleting the index $ci ", contents)
154+
@test occursin("## Fixing this error", contents)
159155
end
160156
return
161157
end
@@ -244,14 +240,11 @@ function test_errors_ModifyNotAllowed_constraint()
244240
change = MOI.ScalarConstantChange(1.0)
245241
err = MOI.ModifyConstraintNotAllowed(ci, change)
246242
@test_throws err MOI.modify(model, ci, change)
247-
@test sprint(showerror, err) ==
248-
"$(MOI.ModifyConstraintNotAllowed{MOI.VariableIndex,MOI.EqualTo{Float64},MOI.ScalarConstantChange{Float64}}):" *
249-
" Modifying the constraints $(MOI.ConstraintIndex{MOI.VariableIndex,MOI.EqualTo{Float64}}(1))" *
250-
" with MathOptInterface.ScalarConstantChange{Float64}(1.0) cannot" *
251-
" be performed. You may want to use a `CachingOptimizer` in" *
252-
" `AUTOMATIC` mode or you may need to call `reset_optimizer`" *
253-
" before doing this operation if the `CachingOptimizer` is in" *
254-
" `MANUAL` mode."
243+
contents = sprint(showerror, err)
244+
@test occursin("$(typeof(err)):" contents)
245+
@test occursin("Modifying the constraints $ci", contents)
246+
@test occursin("## Fixing this error", contents)
247+
return
255248
end
256249

257250
function test_errors_ModifyNotAllowed_objective()
@@ -260,13 +253,11 @@ function test_errors_ModifyNotAllowed_objective()
260253
attr = MOI.ObjectiveFunction{MOI.VariableIndex}()
261254
err = MOI.ModifyObjectiveNotAllowed(change)
262255
@test_throws err MOI.modify(model, attr, change)
263-
@test sprint(showerror, err) ==
264-
"$(MOI.ModifyObjectiveNotAllowed{MOI.ScalarConstantChange{Float64}}):" *
265-
" Modifying the objective function with $(MOI.ScalarConstantChange{Float64}(1.0))" *
266-
" cannot be performed. You may want to use a `CachingOptimizer`" *
267-
" in `AUTOMATIC` mode or you may need to call `reset_optimizer`" *
268-
" before doing this operation if the `CachingOptimizer` is in" *
269-
" `MANUAL` mode."
256+
contents = sprint(showerror, err)
257+
@test occursin("$(typeof(err)):" contents)
258+
@test occursin("Modifying the objective function with $change", contents)
259+
@test occursin("## Fixing this error", contents)
260+
return
270261
end
271262

272263
function test_errors_show_SetAttributeNotAllowed()
@@ -352,11 +343,10 @@ function test_get_fallback_error()
352343
MOI.get(model, MOI.SolveTimeSec()),
353344
)
354345
err = MOI.GetAttributeNotAllowed(MOI.SolveTimeSec(), "")
355-
@test sprint(showerror, err) ==
356-
"$(typeof(err)): Getting attribute $(MOI.SolveTimeSec()) cannot be " *
357-
"performed. You may want to use a `CachingOptimizer` in " *
358-
"`AUTOMATIC` mode or you may need to call `reset_optimizer` before " *
359-
"doing this operation if the `CachingOptimizer` is in `MANUAL` mode."
346+
contents = sprint(showerror, err)
347+
@test occursin("$(typeof(err)):", contents)
348+
@test occursin("Getting attribute $(MOI.SolveTimeSec())", contents)
349+
@test occursin("## Fixing this error", contents)
360350
return
361351
end
362352

0 commit comments

Comments
 (0)