Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Exception Handling in _get_yaml_data_and_path(...) #392

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/wireviz/wireviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,17 @@ def _get_yaml_data_and_path(inp: Union[str, Path, Dict]) -> (Dict, Path):
yaml_path = Path(inp).expanduser().resolve(strict=True)
# if no FileNotFoundError exception happens, get file contents
yaml_str = open_file_read(yaml_path).read()
except (FileNotFoundError, OSError) as e:
# if inp is a long YAML string, Pathlib will raise OSError: [errno.ENAMETOOLONG]
# (in Windows, it seems OSError [errno.EINVAL] might be raised in some cases)
# when trying to expand and resolve it as a path.
# Catch this error, but raise any others
except (FileNotFoundError, OSError, ValueError) as e:
# if inp is a long YAML string, Pathlib will normally raise
# FileNotFoundError or OSError(errno = ENAMETOOLONG) when
# trying to expand and resolve it as a path, but in Windows
# might ValueError or OSError(errno = EINVAL or None) be raised
# instead in some cases (depending on the Python version).
# Catch these specific errors, but raise any others.

from errno import EINVAL, ENAMETOOLONG

if type(e) is OSError and e.errno not in (EINVAL, ENAMETOOLONG):
if type(e) is OSError and e.errno not in (EINVAL, ENAMETOOLONG, None):
raise e
# file does not exist; assume inp is a YAML string
yaml_str = inp
Expand Down
Loading