Repository: ManimCommunity/manim

Issue:
Raise error when --config_file does not find a valid config file
Using the --config_file flag is really nice to customize manim scripts, e.g. with:
--config_file 'home/manim_settings.cfg'
However, if the config file is not found, manim goes currently automatically back to the default config.
Is this a bug or a feature?
In my opinion, it would be ok to raise an error like
`Error: --config_file could not find a valid config file.`
Or if not an error, at least a warning.


Discussion:
Sounds more like a bug to me. If greenlighted by a project moderator, I can investigate where this behavior comes from and write a bugfix. I'll go and double-check the protocol I should follow to get started for now.
seems more like a non-implemented feature than a bug. 

I totally concur with the idea!
I agree, there should be a warning/error and it should probably be done [here](https://github.com/ManimCommunity/manim/blob/master/manim/_config/utils.py#L740-L741).

The automatic fallback to the default config file is currently an unspecified [feature](https://docs.manim.community/en/v0.3.0/tutorials/configuration.html#order-of-operations) and I'd prefer raising a FileNotFoundError instead of a warning that it''ll instead use the default.cfg.
Well said, @huguesdevimeux! Thank you the directions, @jsonvillanueva. 
I’ll start tracing the fallback call and see how to make it raise an error instead.

> I’ll start tracing the fallback call

There isn't really a fallback call, it's just this function parses multiple config files (library-wide, system-wide, and then finally the folder-wide/custom config files) and uses the values in these .cfg files to set the values of a dictionary-like object (ManimConfig). 

All that may need to be done is to check that the passed in filename actually exists on the system just before [L740](https://github.com/ManimCommunity/manim/blob/master/manim/_config/utils.py#L740)

Now I also think an error message is the way to go. @Alexis-Lionel : Cool that you want to work on this. In case you need assistence on the pr, please let us know!
I had forgotten to assign you @Alexis-Lionel; do let us know if you run into any issues!
Thanks a lot, @jsonvillanueva and @kolibril13! A simple path check prior to [L740](https://github.com/ManimCommunity/manim/blob/master/manim/_config/utils.py#L740) did indeed work - 
```
if not os.path.isfile(filename):
            raise FileNotFoundError(errno.ENOENT, "Error: --config_file could not find a valid config file.", filename)
```
I also had to `import errno` to process the errno.ENOENT call, but not sure if it's better than hardcoding the '2' in there :D

The other thing bothering me is the gargantuan traceback it produces - the actual error is at the very end of it and may be hard to spot -

> [lionel@ArchFrame6970 manproj]$ manim scene.py --config_file fsafg.cfg 
Manim Community v0.3.0
Traceback (most recent call last):
  File "/home/lionel/.local/bin/manim", line 8, in <module>
    sys.exit(main())
  File "/home/lionel/.local/lib/python3.9/site-packages/manim/__main__.py", line 74, in main
    config.digest_args(args)
  File "/home/lionel/.local/lib/python3.9/site-packages/manim/_config/utils.py", line 607, in digest_args
    self.digest_file(args.config_file)
  File "/home/lionel/.local/lib/python3.9/site-packages/manim/_config/utils.py", line 742, in digest_file
    raise FileNotFoundError(errno.ENOENT, "Error: --config_file could not find a valid config file.", filename)
FileNotFoundError: [Errno 2] Error: --config_file could not find a valid config file.: 'fsafg.cfg'

I get a feeling this can be improved somehow, but will gladly PR the current version with your approval.


I think this version is good to submit a PR! The large traceback is necessary as it tells the user which files/functions/specific lines were in the stack frame when producing the error (the actual error producing code is at always at the bottom, but the stuff above gives developers hints where else to look for the bug -- if it is a bug).

I think if we're looking for brevity, then we would instead use `logging.error()` followed by a `sys.exit()`. As long as manim ultimately exits, I think either implementation is fine.

Resolving patch:
diff --git a/manim/_config/utils.py b/manim/_config/utils.py
--- a/manim/_config/utils.py
+++ b/manim/_config/utils.py
@@ -14,6 +14,7 @@
 import copy
 import logging
 import os
+import errno
 import sys
 import typing
 from collections.abc import Mapping, MutableMapping
@@ -737,6 +738,13 @@ def digest_file(self, filename: str) -> "ManimConfig":
         multiple times.
 
         """
+        if not os.path.isfile(filename):
+            raise FileNotFoundError(
+                errno.ENOENT,
+                "Error: --config_file could not find a valid config file.",
+                filename,
+            )
+
         if filename:
             return self.digest_parser(make_config_parser(filename))
 


Test patch: