Skip to content

Commit

Permalink
Move try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnugraha committed Jan 31, 2025
1 parent 4e3b93f commit 806aadd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
17 changes: 3 additions & 14 deletions src/py/flwr/cli/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,9 @@ def validate_federation_in_project_config(

# Override the federation configuration if provided
if overrides:
try:
overrides_dict = parse_config_args(overrides, flatten=False)
federation_config = fuse_dicts(federation_config, overrides_dict)
except tomli.TOMLDecodeError as e:
typer.secho(
f"❌ Error parsing overrides due to invalid format. Ensure that "
"your overrides uses supported types of bool, int, string, or "
"float, e.g. true/false, \"random string\", or 123 and is formatted "
"correctly, e.g. 'key1=value1 key2=value2' or 'key3=value3'.",
fg=typer.colors.RED,
bold=True,
)
raise typer.Exit(code=1)

overrides_dict = parse_config_args(overrides, flatten=False)
federation_config = fuse_dicts(federation_config, overrides_dict)

return federation, federation_config


Expand Down
16 changes: 14 additions & 2 deletions src/py/flwr/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from typing import IO, Any, Optional, TypeVar, Union, cast, get_args

import tomli
import typer

from flwr.common.constant import (
APP_DIR,
Expand Down Expand Up @@ -233,8 +234,19 @@ def parse_config_args(

matches = pattern.findall(config_line)
toml_str = "\n".join(f"{k} = {v}" for k, v in matches)
overrides.update(tomli.loads(toml_str))
flat_overrides = flatten_dict(overrides) if flatten else overrides
try:
overrides.update(tomli.loads(toml_str))
flat_overrides = flatten_dict(overrides) if flatten else overrides
except tomli.TOMLDecodeError as err:
typer.secho(
"❌ Error parsing overrides due to invalid format. Ensure that "
"your overrides uses supported types of bool, int, string, or "
'float, e.g. true/false, "random string", or 123 and is formatted '
"correctly, e.g. 'key1=value1 key2=value2' or 'key3=value3'.",
fg=typer.colors.RED,
bold=True,
)
raise typer.Exit(code=1) from err

return flat_overrides

Expand Down

0 comments on commit 806aadd

Please sign in to comment.