Skip to content

Commit 18b8dc1

Browse files
committed
feat: add debug exceptions
1 parent e753056 commit 18b8dc1

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

fortls/__init__.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,34 @@
1616
__all__ = ["__version__"]
1717

1818

19-
def error_exit(error_str: str):
20-
print(f"ERROR: {error_str}")
21-
sys.exit(-1)
19+
class DebugError(Exception):
20+
"""Base class for debug CLI."""
21+
22+
23+
class ParameterError(DebugError):
24+
"""Exception raised for errors in the parameters."""
2225

2326

2427
def main():
2528
freeze_support()
2629
args = cli(__name__).parse_args()
2730

28-
if args.debug_parser:
29-
debug_server_parser(args)
31+
try:
32+
if args.debug_parser:
33+
debug_server_parser(args)
3034

31-
elif is_debug_mode(args):
32-
debug_lsp(args, vars(args))
35+
elif is_debug_mode(args):
36+
debug_lsp(args, vars(args))
3337

34-
else:
35-
stdin, stdout = sys.stdin.buffer, sys.stdout.buffer
36-
LangServer(
37-
conn=JSONRPC2Connection(ReadWriter(stdin, stdout)),
38-
settings=vars(args),
39-
).run()
38+
else:
39+
stdin, stdout = sys.stdin.buffer, sys.stdout.buffer
40+
LangServer(
41+
conn=JSONRPC2Connection(ReadWriter(stdin, stdout)),
42+
settings=vars(args),
43+
).run()
44+
except DebugError as e:
45+
print(f"ERROR: {e}")
46+
sys.exit(-1)
4047

4148

4249
def is_debug_mode(args):
@@ -86,7 +93,7 @@ def debug_lsp(args, settings):
8693

8794
def debug_rootpath(args, server):
8895
if not os.path.isdir(args.debug_rootpath):
89-
error_exit("'debug_rootpath' not specified for debug request")
96+
raise DebugError("'debug_rootpath' not specified for debug request")
9097
print('\nTesting "initialize" request:')
9198
print(f' Root = "{args.debug_rootpath}"')
9299
server.serve_initialize({"params": {"rootPath": args.debug_rootpath}})
@@ -147,7 +154,7 @@ def debug_symbols(args, server):
147154
def debug_workspace_symbols(args, server):
148155
print('\nTesting "workspace/symbol" request:')
149156
if args.debug_rootpath is None:
150-
error_exit("'debug_rootpath' not specified for debug request")
157+
raise DebugError("'debug_rootpath' not specified for debug request")
151158
results = server.serve_workspace_symbol(
152159
{"params": {"query": args.debug_workspace_symbols}}
153160
)
@@ -502,7 +509,7 @@ def locate_config(root: str) -> str | None:
502509
file_obj = FortranFile(args.debug_filepath, pp_suffixes)
503510
err_str, _ = file_obj.load_from_disk()
504511
if err_str:
505-
error_exit(f"Reading file failed: {err_str}")
512+
raise DebugError(f"Reading file failed: {err_str}")
506513
print(f" Detected format: {'fixed' if file_obj.fixed else 'free'}")
507514
print("\n=========\nParser Output\n=========\n")
508515
file_ast = file_obj.parse(debug=True, pp_defs=pp_defs, include_dirs=include_dirs)
@@ -518,18 +525,18 @@ def locate_config(root: str) -> str | None:
518525
def ensure_file_accessible(filepath: str):
519526
"""Ensure the file exists and is accessible, raising an error if not."""
520527
if not os.path.isfile(filepath):
521-
error_exit(f"File '{filepath}' does not exist or is not accessible")
528+
raise DebugError(f"File '{filepath}' does not exist or is not accessible")
522529
print(f' File = "{filepath}"')
523530

524531

525532
def check_request_params(args, loc_needed=True):
526533
ensure_file_accessible(args.debug_filepath)
527534
if loc_needed:
528535
if args.debug_line is None:
529-
error_exit("'debug_line' not specified for debug request")
536+
raise ParameterError("'debug_line' not specified for debug request")
530537
print(f" Line = {args.debug_line}")
531538
if args.debug_char is None:
532-
error_exit("'debug_char' not specified for debug request")
539+
raise ParameterError("'debug_char' not specified for debug request")
533540
print(f" Char = {args.debug_char}\n")
534541

535542

0 commit comments

Comments
 (0)