Skip to content

fix: correctly report type errors in config - #25

Merged
moverest merged 1 commit into
moverest:masterfrom
cljoly:cj/config-error-fix
Jul 20, 2025
Merged

moverest merged 1 commit into
moverest:masterfrom
cljoly:cj/config-error-fix

Conversation

@cljoly

@cljoly cljoly commented Jul 20, 2025

Copy link
Copy Markdown
Contributor

Issue

Currently, the types of fields in the configuration is validated when
the configuration is loaded. But the error is not reported correctly:

Traceback (most recent call last):
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 922, in <module>
    main()
    ~~~~^^
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 830, in main
    config = Config(args.config)
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 174, in __init__
    errors = list(self._validate("", self.config))
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 58, in validate
    yield from field_validators[field_name](f"{path}.{field_name}", field_value)
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 58, in validate
    yield from field_validators[field_name](f"{path}.{field_name}", field_value)
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 68, in validate
    path, f"Type {type(value)} is not one of {', '.join(types)}."
                                              ~~~~~~~~~^^^^^^^
TypeError: sequence item 0: expected str instance, type found

Here, an exception occurs when trying to instantiate ValidationError,
because the type Type is not a str, which join expects.

This MR fixes that, so we now print:

Traceback (most recent call last):
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 922, in <module>
    main()
    ~~~~^^
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 830, in main
    config = Config(args.config)
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 176, in __init__
    raise ConfigError(errors)
ConfigError: Config errors:
 .screenshot.cursor: Type <class 'str'> is not one of <class 'bool'>.

Testing

I triggered the error with the following config:

[screenshot]
cursor = "false2"

Issue
-----

Currently, the types of fields in the configuration is validated when
the configuration is loaded. But the error is not reported correctly:
```
Traceback (most recent call last):
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 922, in <module>
    main()
    ~~~~^^
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 830, in main
    config = Config(args.config)
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 174, in __init__
    errors = list(self._validate("", self.config))
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 58, in validate
    yield from field_validators[field_name](f"{path}.{field_name}", field_value)
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 58, in validate
    yield from field_validators[field_name](f"{path}.{field_name}", field_value)
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 68, in validate
    path, f"Type {type(value)} is not one of {', '.join(types)}."
                                              ~~~~~~~~~^^^^^^^
TypeError: sequence item 0: expected str instance, type found
```
Here, an exception occurs when trying to instantiate `ValidationError`,
because the type `Type` is not a `str`, which `join` expects.

This MR fixes that, so we now print:
```
Traceback (most recent call last):
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 922, in <module>
    main()
    ~~~~^^
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 830, in main
    config = Config(args.config)
  File "/home/cjoly/ghq/github.com/moverest/sway-interactive-screenshot/./sway-interactive-screenshot", line 176, in __init__
    raise ConfigError(errors)
ConfigError: Config errors:
 .screenshot.cursor: Type <class 'str'> is not one of <class 'bool'>.
```

Testing
-------

I triggered the error with the following config:
```toml
[screenshot]
cursor = "false2"
```
@moverest
moverest merged commit 897d700 into moverest:master Jul 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants