The call from_dict(MyType, dict_wrongkey) will throw an InvalidKeyError with a descriptive message that mentions
the invalid key,
ERROR: InvalidKeyError: invalid key mystr, possible keys are: str
whereas the latter gives a MethodError where one cannot see which parameter of the dictionary failed the conversion,
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type String
Workaround
A workaround for this would be to overload the from_dict method generically, e.g.
function Configurations.from_dict(::Type{OptionType}, ::OptionField{f_name}, ::Type{T}, x::S) where {OptionType, f_name, T, S}
try
return convert(T, x)
catch e
if e isa MethodError && e.f == convert
error("Failed to convert '$x' to type '$T' for field '$f_name'")
end
end
end
However, this feels a little hacky, because I think the exception handling should be separated from the conversion method.
Question: Would you be interested in reviewing a merge request that implements a new error type similar to InvalidKeyError?
Hi,
I might have a suggestions for an improvement of default error handling in terms of descriptiveness.
Example
The call
from_dict(MyType, dict_wrongkey)
will throw anInvalidKeyError
with a descriptive message that mentions the invalid key,whereas the latter gives a
MethodError
where one cannot see which parameter of the dictionary failed the conversion,Workaround
A workaround for this would be to overload the
from_dict
method generically, e.g.However, this feels a little hacky, because I think the exception handling should be separated from the conversion method.
Question: Would you be interested in reviewing a merge request that implements a new error type similar to
InvalidKeyError
?Cheers