Skip to content

Commit

Permalink
Merge pull request #2844 from CliMA/glw/yaml-error
Browse files Browse the repository at this point in the history
Throw a more informative error for unsupported config dictionary entries
  • Loading branch information
glwagner authored Mar 28, 2024
2 parents b56888a + 95ae36e commit d6f2aa6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/solver/model_getters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ end

function get_model_config(parsed_args)
config = parsed_args["config"]
@assert config in ("sphere", "column", "box", "plane")

valid_configurations = ("sphere", "column", "box", "plane")

if !(config valid_configurations)
error_message = string(
"config = $config is not one of the ",
"valid configurations $valid_configurations",
)
throw(ArgumentError(error_message))
end

return if config == "sphere"
SphericalModel()
elseif config == "column"
Expand Down
12 changes: 11 additions & 1 deletion src/solver/yaml_helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,17 @@ function override_default_config(config_dict::AbstractDict;)
for k in intersect(keys(config_dict), keys(default_config))
default_type = typeof(default_config[k])
v = config_dict[k]
config[k] = isnothing(default_config[k]) ? v : default_type(v)

# Attempt to convert user value `v` to the same type as
# the default. If that fails, throw an informative error.
config[k] = try
isnothing(default_config[k]) ? v : default_type(v)
catch err
user_entry_type = typeof(v)
msg = """Configuration entry "$(k)" = $v has type $(user_entry_type),
but must have type $default_type."""
throw(ArgumentError(msg))
end
end

# The "diagnostics" entry is a more complex type that doesn't fit the schema described in
Expand Down

0 comments on commit d6f2aa6

Please sign in to comment.