Skip to content

Commit

Permalink
Enhance handling syntax errors with TRY/EXCEPT.
Browse files Browse the repository at this point in the history
Show unexecuted TRY/EXCEPT branches if there are syntax errors.

Make invalid TRY/EXCEPT itself a syntax error. Fixes #4484.
  • Loading branch information
pekkaklarck committed Sep 28, 2022
1 parent 192d26e commit 2f67f8f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
3 changes: 3 additions & 0 deletions atest/robot/running/try_except/invalid_try_except.robot
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@ CONTINUE in FINALLY

RETURN in FINALLY
TRY:PASS FINALLY:FAIL path=body[0].body[0]

Invalid TRY/EXCEPT causes syntax error that cannot be caught
TRY:FAIL EXCEPT:NOT RUN ELSE:NOT RUN
2 changes: 1 addition & 1 deletion atest/robot/running/try_except/try_except.robot
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Default except pattern
FAIL PASS

Syntax errors cannot be caught
FAIL
FAIL NOT RUN NOT RUN

Finally block executed when no failures
[Template] None
Expand Down
13 changes: 13 additions & 0 deletions atest/testdata/running/try_except/invalid_try_except.robot
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,19 @@ RETURN in FINALLY
[Documentation] FAIL RETURN cannot be used in FINALLY branch.
RETURN in FINALLY

Invalid TRY/EXCEPT causes syntax error that cannot be caught
[Documentation] FAIL TRY branch cannot be empty.
TRY
TRY
EXCEPT
Fail Not run
END
EXCEPT
Fail Not run because error cannot be caught
ELSE
Fail Not run either
END

*** Keywords ***
RETURN in FINALLY
TRY
Expand Down
2 changes: 2 additions & 0 deletions atest/testdata/running/try_except/try_except.robot
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ Syntax errors cannot be caught
${y} = ${x} Set Variable
EXCEPT
Fail Should not be run
ELSE
Fail Should not be run
END

Finally block executed when no failures
Expand Down
10 changes: 4 additions & 6 deletions src/robot/running/bodyrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ def run(self, data):
with StatusReporter(data, TryResult(), self._context, run):
if data.error:
self._run_invalid(data)
return False
return
error = self._run_try(data, run)
run_excepts_or_else = self._should_run_excepts_or_else(error, run)
if error:
Expand All @@ -504,8 +504,8 @@ def _run_invalid(self, data):
runner.run(branch.body)
if not error_reported:
error_reported = True
raise ExecutionFailed(data.error)
raise ExecutionFailed(data.error)
raise DataError(data.error, syntax=True)
raise ExecutionFailed(data.error, syntax=True)

def _run_try(self, data, run):
result = TryBranchResult(data.TRY)
Expand All @@ -516,7 +516,7 @@ def _should_run_excepts_or_else(self, error, run):
return False
if not error:
return True
return not (error.skip or isinstance(error, ExecutionPassed))
return not (error.skip or error.syntax or isinstance(error, ExecutionPassed))

def _run_branch(self, branch, result, run=True, error=None):
try:
Expand All @@ -526,8 +526,6 @@ def _run_branch(self, branch, result, run=True, error=None):
runner = BodyRunner(self._context, run, self._templated)
runner.run(branch.body)
except ExecutionStatus as err:
if isinstance(err, ExecutionFailed) and err.syntax:
raise err
return err
else:
return None
Expand Down
2 changes: 1 addition & 1 deletion src/robot/running/statusreporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
context.test.status = result.status
result.endtime = get_timestamp()
context.end_keyword(ModelCombiner(self.data, result))
if failure is not exc_val:
if failure is not exc_val and not self.suppress:
raise failure
return self.suppress

Expand Down

0 comments on commit 2f67f8f

Please sign in to comment.