Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,37 @@ operation_name(err::GetAttributeNotAllowed) = "Getting attribute $(err.attr)"

message(err::GetAttributeNotAllowed) = err.message

function fix_message(err::GetAttributeNotAllowed)
if is_set_by_optimize(err.attr)
return """
## Fixing this error

This error occurs when we cannot query the attribute from the solver.
No other information is available. Check the solver log for details.
"""
end
return """
## Fixing this error

An `MOI.NotAllowedError` error occurs when you have tried to do something that
is not implemented by the solver.

The most common way to fix this error is to wrap the optimizer in a
`MOI.Utilities.CachingOptimizer`.

For example, if you are using `JuMP.Model` or `JuMP.set_optimizer`, do:
```julia
model = JuMP.Model(optimizer; with_cache_type = Float64)
model = JuMP.GenericModel{T}(optimizer; with_cache_type = T)
JuMP.set_optimizer(model, optimizer; with_cache_type = Float64)
```
Similarly, if you are using `MOI.instantiate`, do:
```julia
model = MOI.instantiate(optimizer; with_cache_type = Float64)
```
"""
end

"""
AbstractSubmittable

Expand Down
47 changes: 24 additions & 23 deletions src/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,33 @@ function Base.showerror(io::IO, err::NotAllowedError)
println(io, " because:\n\n", m)
end
println(io)
print(
io,
"""
## Fixing this error

An `MOI.NotAllowedError` error occurs when you have tried to do something that
is not implemented by the solver.

The most common way to fix this error is to wrap the optimizer in a
`MOI.Utilities.CachingOptimizer`.

For example, if you are using `JuMP.Model` or `JuMP.set_optimizer`, do:
```julia
model = JuMP.Model(optimizer; with_cache_type = Float64)
model = JuMP.GenericModel{T}(optimizer; with_cache_type = T)
JuMP.set_optimizer(model, optimizer; with_cache_type = Float64)
```
Similarly, if you are using `MOI.instantiate`, do:
```julia
model = MOI.instantiate(optimizer; with_cache_type = Float64)
```
""",
)
print(io, fix_message(err))
return
end

function fix_message(::NotAllowedError)
return """
## Fixing this error

An `MOI.NotAllowedError` error occurs when you have tried to do something that
is not implemented by the solver.

The most common way to fix this error is to wrap the optimizer in a
`MOI.Utilities.CachingOptimizer`.

For example, if you are using `JuMP.Model` or `JuMP.set_optimizer`, do:
```julia
model = JuMP.Model(optimizer; with_cache_type = Float64)
model = JuMP.GenericModel{T}(optimizer; with_cache_type = T)
JuMP.set_optimizer(model, optimizer; with_cache_type = Float64)
```
Similarly, if you are using `MOI.instantiate`, do:
```julia
model = MOI.instantiate(optimizer; with_cache_type = Float64)
```
"""
end

"""
message(err::Union{UnsupportedError, NotAllowedError})

Expand Down
13 changes: 13 additions & 0 deletions test/General/errors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ function test_get_fallback_error()
contents,
)
@test occursin("## Fixing this error", contents)
@test occursin("No other information is available", contents)
return
end

Expand Down Expand Up @@ -409,6 +410,18 @@ function test_logs_precompile()
return
end

function test_GetAttributeNotAllowed_showerror()
c_set = sprint(showerror, MOI.GetAttributeNotAllowed(MOI.ConstraintSet()))
@test occursin("## Fixing this error", c_set)
@test occursin("MOI.Utilities.CachingOptimizer", c_set)
@test !occursin("Check the solver log for details.", c_set)
c_dual = sprint(showerror, MOI.GetAttributeNotAllowed(MOI.ConstraintDual()))
@test occursin("## Fixing this error", c_dual)
@test !occursin("MOI.Utilities.CachingOptimizer", c_dual)
@test occursin("Check the solver log for details.", c_dual)
return
end

end # module

TestErrors.runtests()
Loading