Skip to content

Unhelpful error message from logging.config.FileConfig can be improved #103606

Closed

Description

@vsajip

Feature or enhancement

If a configuration file does not exist, the error raised by logging.config.fileConfig doesn't say that the file doesn't exist, instead suggesting that the file has an invalid format. This should be improved so that the correct error message is provided.

Pitch

The improved error message will guide a user to an error such as a typo in the filename more quickly. See for example this question.

Linked PRs

Activity

  1. added
    type-feature
    on Apr 18, 2023
  2. self-assigned this
    on Apr 18, 2023
  3. Agent-Hellboy commented on Apr 19, 2023

    @Agent-Hellboy
    Contributor

    Hi, @vsajip can I try to fix this?

  4. arhadthedev commented on Apr 19, 2023

    @arhadthedev
    Member

    @Agent-Hellboy It's an open project, no preliminary assignment is required. The only thing you need is to follow the contributor's manual at https://devguide.python.org/ where conventions and processes are described.

  5. Agent-Hellboy commented on Apr 19, 2023

    @Agent-Hellboy
    Contributor

    okay, sure

  6. Agent-Hellboy commented on Apr 19, 2023

    @Agent-Hellboy
    Contributor

    Hi @arhadthedev
    my approach would be to check the return value of cp.read(fname, encoding=encoding)

    cp.read(fname, encoding=encoding)

    It uses configparser to parse the configuration files which suppresses the OSError but it returns an empty list if the file is not present
    def read(self, filenames, encoding=None):

    I will raise a PR which will raise ValueError that the file is not present and will try to include a test for the same

  7. added a commit that references this issue on Apr 19, 2023
  8. vsajip commented on Apr 20, 2023

    @vsajip
    MemberAuthor

    Wouldn't it be simpler to use e.g. os.path.exists() and then throw a FileNotFoundError if the file doesn't exist? That would be the approach I would have taken.

  9. arhadthedev commented on Apr 20, 2023

    @arhadthedev
    Member

    Because of the easier to ask for forgiveness than permission principle with the very os.path.exists() as an example:

    import os
    
    # violates EAFP coding style
    if os.path.exists("file.txt"):
        os.unlink("file.txt")
  10. vsajip commented on Apr 20, 2023

    @vsajip
    MemberAuthor

    Yes, but the "file not found" message/FileNotFound exception are more informative than a generic "invalid file" when the file doesn't exist. It's a small point, I suppose; it's easier to ask for forgiveness, but that doesn't mean it's a cast-iron rule.

  11. Agent-Hellboy commented on Apr 20, 2023

    @Agent-Hellboy
    Contributor

    okay, I will raise FileNotFound before giving the file to the config parser

    if isinstance(fname, configparser.RawConfigParser):

    why should I try to parse a file which is not present

  12. added a commit that references this issue on Apr 20, 2023
  13. added a commit that references this issue on May 18, 2023
  14. added a commit that references this issue on May 20, 2023
  15. gpshead commented on May 20, 2023

    @gpshead
    Member

    I left some comments on the merged PR... ValueError isn't the right exception to use for things external to the process not related to the variable value itself. I suggest RuntimeError if you don't have your own Error exception class.

  16. Agent-Hellboy commented on May 20, 2023

    @Agent-Hellboy
    Contributor

    sure @gpshead, I will raise a PR

  17. added a commit that references this issue on May 20, 2023
  18. added 2 commits that reference this issue on May 20, 2023
  19. gpshead commented on May 21, 2023

    @gpshead
    Member

    thanks!

  20. added
    3.11
    3.12
    on May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.113.12type-feature

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions