Skip to content

Commit 446dafe

Browse files
committed
Handle diagnostics if getting the tokens of a document fails with an exception
1 parent baf0c2b commit 446dafe

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

robotcode/language_server/robotframework/parts/diagnostics.py

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,53 @@ async def collect_token_errors(self, sender: Any, document: TextDocument) -> Dia
6363
from robot.parsing.lexer.tokens import Token
6464

6565
result: List[Diagnostic] = []
66+
try:
67+
for token in await self.parent.documents_cache.get_tokens(document):
68+
if token.type in [Token.ERROR, Token.FATAL_ERROR]:
69+
result.append(self._create_error_from_token(token))
70+
71+
try:
72+
for variable_token in token.tokenize_variables():
73+
if variable_token == token:
74+
break
75+
76+
if variable_token.type in [Token.ERROR, Token.FATAL_ERROR]:
77+
result.append(self._create_error_from_token(variable_token))
78+
79+
except VariableError as e:
80+
result.append(
81+
Diagnostic(
82+
range=range_from_token(token),
83+
message=str(e),
84+
severity=DiagnosticSeverity.ERROR,
85+
source=self.source_name,
86+
code=type(e).__qualname__,
87+
)
88+
)
6689

67-
for token in await self.parent.documents_cache.get_tokens(document):
68-
if token.type in [Token.ERROR, Token.FATAL_ERROR]:
69-
result.append(self._create_error_from_token(token))
70-
71-
try:
72-
for variable_token in token.tokenize_variables():
73-
if variable_token == token:
74-
break
75-
76-
if variable_token.type in [Token.ERROR, Token.FATAL_ERROR]:
77-
result.append(self._create_error_from_token(variable_token))
78-
79-
except VariableError as e:
80-
result.append(
90+
return DiagnosticsResult(self.collect_token_errors, result)
91+
except BaseException as e:
92+
return DiagnosticsResult(
93+
self.collect_token_errors,
94+
[
8195
Diagnostic(
82-
range=range_from_token(token),
83-
message=str(e),
96+
range=Range(
97+
start=Position(
98+
line=0,
99+
character=0,
100+
),
101+
end=Position(
102+
line=len(document.lines),
103+
character=len(document.lines[-1] or ""),
104+
),
105+
),
106+
message=f"Fatal {type(e).__qualname__}: {e}",
84107
severity=DiagnosticSeverity.ERROR,
85108
source=self.source_name,
86109
code=type(e).__qualname__,
87110
)
88-
)
89-
90-
return DiagnosticsResult(self.collect_token_errors, result)
111+
],
112+
)
91113

92114
@language_id("robotframework")
93115
@_logger.call
@@ -145,7 +167,6 @@ async def collect_walk_model_errors(self, sender: Any, document: TextDocument) -
145167
@language_id("robotframework")
146168
@_logger.call
147169
async def collect_namespace_diagnostics(self, sender: Any, document: TextDocument) -> DiagnosticsResult:
148-
149170
namespace = await self.parent.documents_cache.get_namespace(document)
150171
if namespace is None:
151172
return DiagnosticsResult(self.collect_namespace_diagnostics, None)

0 commit comments

Comments
 (0)