Skip to content

Commit

Permalink
refactor: use more specific exceptions for not found files
Browse files Browse the repository at this point in the history
  • Loading branch information
gnikit committed Apr 28, 2024
1 parent 948fbd1 commit dc50bec
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
10 changes: 7 additions & 3 deletions fortls/langserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@
load_intrinsics,
set_lowercase_intrinsics,
)
from fortls.parsers.internal.parser import FortranFile, ParserError, get_line_context
from fortls.parsers.internal.parser import (
FortranFile,
FortranFileNotFoundError,
get_line_context,
)
from fortls.parsers.internal.scope import Scope
from fortls.parsers.internal.use import Use
from fortls.parsers.internal.utilities import (
Expand Down Expand Up @@ -1396,7 +1400,7 @@ def update_workspace_file(
file_changed = file_obj.load_from_disk()
if not file_changed:
return False, None
except ParserError as exc:
except FortranFileNotFoundError as exc:
log.error("%s : %s", str(exc), filepath)
raise LSPError from exc

Check warning on line 1405 in fortls/langserver.py

View check run for this annotation

Codecov / codecov/patch

fortls/langserver.py#L1403-L1405

Added lines #L1403 - L1405 were not covered by tests

Expand Down Expand Up @@ -1459,7 +1463,7 @@ def file_init(
# TODO: allow to bubble up the error message
try:
file_obj.load_from_disk()
except ParserError as e:
except FortranFileNotFoundError as e:
return str(e)

Check warning on line 1467 in fortls/langserver.py

View check run for this annotation

Codecov / codecov/patch

fortls/langserver.py#L1466-L1467

Added lines #L1466 - L1467 were not covered by tests
try:
# On Windows multiprocess does not propagate global variables in a shell.
Expand Down
15 changes: 8 additions & 7 deletions fortls/parsers/internal/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,15 +882,16 @@ def load_from_disk(self) -> bool:
Raises
------
FileReadDecodeError
If the file could not be read or decoded
FortranFileNotFoundError
If the file could not be found
"""
contents: str
try:
# errors="replace" prevents UnicodeDecodeError being raised
with open(self.path, encoding="utf-8", errors="replace") as f:
contents = re.sub(r"\t", r" ", f.read())
except OSError as exc:
raise FileReadDecodeError("Could not read/decode file") from exc
except FileNotFoundError as exc:
raise FortranFileNotFoundError(exc) from exc
# Check if files are the same
try:
hash = hashlib.md5(
Expand Down Expand Up @@ -2274,7 +2275,7 @@ def append_multiline_macro(def_value: str | tuple, line: str):
debug=debug,
)
log.debug("!!! Completed parsing include file")
except ParserError as e:
except FortranFileNotFoundError as e:

Check warning on line 2278 in fortls/parsers/internal/parser.py

View check run for this annotation

Codecov / codecov/patch

fortls/parsers/internal/parser.py#L2278

Added line #L2278 was not covered by tests
log.debug("!!! Failed to parse include file: %s", str(e))
else:
log.debug(
Expand Down Expand Up @@ -2316,5 +2317,5 @@ class ParserError(Exception):
"""Parser base class exception"""


class FileReadDecodeError(ParserError):
"""File could not be read/decoded"""
class FortranFileNotFoundError(ParserError, FileNotFoundError):
"""File not found"""
4 changes: 2 additions & 2 deletions test/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from setup_tests import test_dir

from fortls.parsers.internal.parser import FileReadDecodeError, FortranFile
from fortls.parsers.internal.parser import FortranFile, FortranFileNotFoundError


def test_line_continuations():
Expand Down Expand Up @@ -34,5 +34,5 @@ def test_submodule():

def test_load_from_disk_exception():
file = FortranFile("/path/to/nonexistent/file.f90")
with pytest.raises(FileReadDecodeError):
with pytest.raises(FortranFileNotFoundError):
file.load_from_disk()

0 comments on commit dc50bec

Please sign in to comment.