Skip to content

Commit 749bbfd

Browse files
committed
Improve error message unsupported attribute for bridges
1 parent acab995 commit 749bbfd

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

src/Bridges/bridge.jl

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ function MOI.supports(
119119
return false
120120
end
121121

122+
function _attribute_error_message(attr, bridge, action)
123+
return "Bridge of type `$(nameof(typeof(bridge)))` does not support " *
124+
"$action the attribute `$attr`. If you encountered this error " *
125+
"unexpectedly, it probably means your model has been " *
126+
"reformulated using the bridge, and you are attempting to query " *
127+
"an attribute that we haven't implemented yet for this bridge. " *
128+
"Please open an issue at https://github.com/jump-dev/MathOptInterface.jl/issues/new " *
129+
"and provide a reproducible example explaining what you were " *
130+
"trying to do."
131+
end
132+
133+
122134
"""
123135
function MOI.get(
124136
model::MOI.ModelLike,
@@ -134,18 +146,8 @@ function MOI.get(
134146
attr::MOI.AbstractConstraintAttribute,
135147
bridge::AbstractBridge,
136148
)
137-
return throw(
138-
ArgumentError(
139-
"Bridge of type `$(typeof(bridge))` does not support accessing " *
140-
"the attribute `$attr`. If you encountered this error " *
141-
"unexpectedly, it probably means your model has been " *
142-
"reformulated using the bridge, and you are attempting to query " *
143-
"an attribute that we haven't implemented yet for this bridge. " *
144-
"Please open an issue at https://github.com/jump-dev/MathOptInterface.jl/issues/new " *
145-
"and provide a reproducible example explaining what you were " *
146-
"trying to do.",
147-
),
148-
)
149+
message = _attribute_error_message(attr, bridge, "accessing")
150+
return throw(ArgumentError(message))
149151
end
150152

151153
function MOI.get(
@@ -173,12 +175,13 @@ function MOI.set(
173175
model::MOI.ModelLike,
174176
attr::MOI.AbstractConstraintAttribute,
175177
bridge::AbstractBridge,
176-
value,
178+
_,
177179
)
180+
message = _attribute_error_message(attr, bridge, "setting a value for")
178181
if MOI.is_copyable(attr) && !MOI.supports(model, attr, typeof(bridge))
179-
throw(MOI.UnsupportedAttribute(attr))
182+
throw(MOI.UnsupportedAttribute(attr, message))
180183
else
181-
throw(MOI.SetAttributeNotAllowed(attr))
184+
throw(MOI.SetAttributeNotAllowed(attr, message))
182185
end
183186
end
184187

test/Bridges/bridge_optimizer.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,13 @@ MOI.Utilities.@model(
218218
(MOI.VectorAffineFunction, MOI.VectorQuadraticFunction)
219219
)
220220

221-
function unsupported_constraint_attribute()
221+
struct AttributeNotAllowed <: MOI.AbstractConstraintAttribute end
222+
223+
function MOI.supports(::MOI.ModelLike, ::AttributeNotAllowed, ::Type{<:MOI.Bridges.Constraint.SplitIntervalBridge})
224+
return true
225+
end
226+
227+
function test_unsupported_constraint_attribute()
222228
mock = MOI.Utilities.MockOptimizer(NoIntervalModel{Float64}())
223229
bridged_mock = MOI.Bridges.Constraint.LessToGreater{Float64}(
224230
MOI.Bridges.Constraint.SplitInterval{Float64}(mock),
@@ -231,22 +237,27 @@ function unsupported_constraint_attribute()
231237
MOI.LessThan{Float64},
232238
}
233239
attr = MOI.Test.UnknownConstraintAttribute()
234-
err = ArgumentError(
235-
"Bridge of type `$(bridge)` does not support accessing " *
240+
message(action) =
241+
"Bridge of type `$(nameof(bridge))` does not support $action " *
236242
"the attribute `$attr`. If you encountered this error " *
237243
"unexpectedly, it probably means your model has been " *
238244
"reformulated using the bridge, and you are attempting to query " *
239245
"an attribute that we haven't implemented yet for this bridge. " *
240246
"Please open an issue at https://github.com/jump-dev/MathOptInterface.jl/issues/new " *
241247
"and provide a reproducible example explaining what you were " *
242-
"trying to do.",
243-
)
248+
"trying to do."
244249
x = MOI.add_variable(bridged_mock)
245250
ci = MOI.add_constraint(bridged_mock, x, MOI.Interval(0.0, 1.0))
246251
@test !MOI.Bridges.is_bridged(bridged_mock, ci)
247252
@test MOI.Bridges.is_bridged(bridged_mock.model, ci)
248253
@test !MOI.supports(bridged_mock, attr, typeof(ci))
254+
err = ArgumentError(message("accessing"))
249255
@test_throws err MOI.get(bridged_mock, attr, ci)
256+
err = MOI.UnsupportedAttribute(attr, message("setting a value for"))
257+
@test_throws err MOI.set(bridged_mock, attr, ci, 1)
258+
attr = AttributeNotAllowed()
259+
err = MOI.SetAttributeNotAllowed(attr, message("setting a value for"))
260+
@test_throws err MOI.set(bridged_mock, attr, ci, 1)
250261
return
251262
end
252263

0 commit comments

Comments
 (0)