Skip to content

Commit 68b15f5

Browse files
committed
refactor: use more specific exceptions for not found files
1 parent e865100 commit 68b15f5

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

fortls/langserver.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@
5151
load_intrinsics,
5252
set_lowercase_intrinsics,
5353
)
54-
from fortls.parsers.internal.parser import FortranFile, ParserError, get_line_context
54+
from fortls.parsers.internal.parser import (
55+
FortranFile,
56+
FortranFileNotFoundError,
57+
get_line_context,
58+
)
5559
from fortls.parsers.internal.scope import Scope
5660
from fortls.parsers.internal.use import Use
5761
from fortls.parsers.internal.utilities import (
@@ -1396,7 +1400,7 @@ def update_workspace_file(
13961400
file_changed = file_obj.load_from_disk()
13971401
if not file_changed:
13981402
return False, None
1399-
except ParserError as exc:
1403+
except FortranFileNotFoundError as exc:
14001404
log.error("%s : %s", str(exc), filepath)
14011405
raise LSPError from exc
14021406

@@ -1459,7 +1463,7 @@ def file_init(
14591463
# TODO: allow to bubble up the error message
14601464
try:
14611465
file_obj.load_from_disk()
1462-
except ParserError as e:
1466+
except FortranFileNotFoundError as e:
14631467
return str(e)
14641468
try:
14651469
# On Windows multiprocess does not propagate global variables in a shell.

fortls/parsers/internal/parser.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -881,15 +881,16 @@ def load_from_disk(self) -> bool:
881881
882882
Raises
883883
------
884-
FileReadDecodeError
885-
If the file could not be read or decoded
884+
FortranFileNotFoundError
885+
If the file could not be found
886886
"""
887887
contents: str
888888
try:
889+
# errors="replace" prevents UnicodeDecodeError being raised
889890
with open(self.path, encoding="utf-8", errors="replace") as f:
890891
contents = re.sub(r"\t", r" ", f.read())
891-
except OSError as exc:
892-
raise FileReadDecodeError("Could not read/decode file") from exc
892+
except FileNotFoundError as exc:
893+
raise FortranFileNotFoundError(exc) from exc
893894
# Check if files are the same
894895
try:
895896
hash = hashlib.md5(
@@ -2274,7 +2275,7 @@ def append_multiline_macro(def_value: str | tuple, line: str):
22742275
debug=debug,
22752276
)
22762277
log.debug("!!! Completed parsing include file")
2277-
except ParserError as e:
2278+
except FortranFileNotFoundError as e:
22782279
log.debug("!!! Failed to parse include file: %s", str(e))
22792280
else:
22802281
log.debug(
@@ -2316,5 +2317,5 @@ class ParserError(Exception):
23162317
"""Parser base class exception"""
23172318

23182319

2319-
class FileReadDecodeError(ParserError):
2320-
"""File could not be read/decoded"""
2320+
class FortranFileNotFoundError(ParserError, FileNotFoundError):
2321+
"""File not found"""

test/test_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
from setup_tests import test_dir
33

4-
from fortls.parsers.internal.parser import FileReadDecodeError, FortranFile
4+
from fortls.parsers.internal.parser import FortranFile, FortranFileNotFoundError
55

66

77
def test_line_continuations():
@@ -51,5 +51,5 @@ def test_end_scopes_semicolon():
5151

5252
def test_load_from_disk_exception():
5353
file = FortranFile("/path/to/nonexistent/file.f90")
54-
with pytest.raises(FileReadDecodeError):
54+
with pytest.raises(FortranFileNotFoundError):
5555
file.load_from_disk()

0 commit comments

Comments
 (0)