diff --git a/src/attributes.jl b/src/attributes.jl index f54123b7e3..7ac70c7183 100644 --- a/src/attributes.jl +++ b/src/attributes.jl @@ -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 diff --git a/src/error.jl b/src/error.jl index d8f8876876..05bb0c16e7 100644 --- a/src/error.jl +++ b/src/error.jl @@ -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}) diff --git a/test/General/errors.jl b/test/General/errors.jl index 33eb14d2c4..7dadbba14f 100644 --- a/test/General/errors.jl +++ b/test/General/errors.jl @@ -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 @@ -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()