16
16
__all__ = ["__version__" ]
17
17
18
18
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."""
22
25
23
26
24
27
def main ():
25
28
freeze_support ()
26
29
args = cli (__name__ ).parse_args ()
27
30
28
- if args .debug_parser :
29
- debug_server_parser (args )
31
+ try :
32
+ if args .debug_parser :
33
+ debug_server_parser (args )
30
34
31
- elif is_debug_mode (args ):
32
- debug_lsp (args , vars (args ))
35
+ elif is_debug_mode (args ):
36
+ debug_lsp (args , vars (args ))
33
37
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 )
40
47
41
48
42
49
def is_debug_mode (args ):
@@ -86,7 +93,7 @@ def debug_lsp(args, settings):
86
93
87
94
def debug_rootpath (args , server ):
88
95
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" )
90
97
print ('\n Testing "initialize" request:' )
91
98
print (f' Root = "{ args .debug_rootpath } "' )
92
99
server .serve_initialize ({"params" : {"rootPath" : args .debug_rootpath }})
@@ -147,7 +154,7 @@ def debug_symbols(args, server):
147
154
def debug_workspace_symbols (args , server ):
148
155
print ('\n Testing "workspace/symbol" request:' )
149
156
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" )
151
158
results = server .serve_workspace_symbol (
152
159
{"params" : {"query" : args .debug_workspace_symbols }}
153
160
)
@@ -502,7 +509,7 @@ def locate_config(root: str) -> str | None:
502
509
file_obj = FortranFile (args .debug_filepath , pp_suffixes )
503
510
err_str , _ = file_obj .load_from_disk ()
504
511
if err_str :
505
- error_exit (f"Reading file failed: { err_str } " )
512
+ raise DebugError (f"Reading file failed: { err_str } " )
506
513
print (f" Detected format: { 'fixed' if file_obj .fixed else 'free' } " )
507
514
print ("\n =========\n Parser Output\n =========\n " )
508
515
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:
518
525
def ensure_file_accessible (filepath : str ):
519
526
"""Ensure the file exists and is accessible, raising an error if not."""
520
527
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" )
522
529
print (f' File = "{ filepath } "' )
523
530
524
531
525
532
def check_request_params (args , loc_needed = True ):
526
533
ensure_file_accessible (args .debug_filepath )
527
534
if loc_needed :
528
535
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" )
530
537
print (f" Line = { args .debug_line } " )
531
538
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" )
533
540
print (f" Char = { args .debug_char } \n " )
534
541
535
542
0 commit comments