Skip to content

knowledge(error-handling): a bare [TryFunction] call propagates its error - #211

Merged
Jesper Schulz-Wedde (JesperSchulz) merged 2 commits into
microsoft:mainfrom
pri-kise:prevent-try-function-wrong-warning
Nov 17, 2026
Merged

Jesper Schulz-Wedde (JesperSchulz) merged 2 commits into
microsoft:mainfrom
pri-kise:prevent-try-function-wrong-warning

Conversation

@pri-kise

Copy link
Copy Markdown
Contributor

Why this correction is needed

The AL review agent keeps flagging bare calls to [TryFunction] procedures as defects. On microsoft/BCApps#8429 it commented three times on the same lines, which call the System Application Xml Validation try-APIs without using their return value:

Comment Agent Claim Basis
r3593827468 v1.7.3, Error Handling, Medium "The caller can never learn whether validation succeeded or failed" cites ignored-tryfunction-return-disables-try-semantics.md
r3932688557 v1.37.6, Error Handling, High "Malformed XML or XSD content will escape as ordinary exceptions instead of letting this code inspect the Boolean result" cites the same article
r3958572924 v1.41.6, Security, Medium "These are Try-prefixed methods, so a parse or schema-validation failure is silently swallowed" agent judgement, no article

All three are wrong, and the third contradicts the first two. The flagged procedure's doc comment says it "Raises an error describing the first schema violation found", and the bare calls do exactly that. The author explained this twice in the PR (1, 2).

Valid pattern: calling a [TryFunction] as a standalone statement when the error should propagate. Without the return value, it is an ordinary call.

Incorrect findings prevented:

  • "the failure is swallowed / ignored / invisible to the caller"
  • "try semantics never activate", reported as a defect when propagation is intended
  • the suggested fix if not Try...() then Error(GetLastErrorText()), which replaces the original error and can move customer content into the message (see privacy/getlasterrortext-customer-content-in-errors.md)

Root causes in the corpus

  • ignored-tryfunction-return-disables-try-semantics.md describes the platform correctly. But its title and Best Practice read as "always consume the result", and only the Anti Pattern carries the real condition ("…and assuming the attribute suppresses its errors").
  • The error-handling leaf's targeted check flagged on call shape alone: a resolved [TryFunction] with an ignored result.
  • In the bad sample, the only evidence of the mistaken assumption was a comment, which fixture preparation strips. So the positive case was literally a bare call, the same shape as intended propagation.
  • The article had no evaluation coverage.
  • performance/use-tryfunction-for-error-catching-not-rollback.md said the result "must be consumed". events/avoid-raising-events-inside-try-functions.md said "A TryFunction catches all errors" without qualification.

What this changes

  • New negative knowledge: error-handling/bare-tryfunction-call-propagates-errors.md. A bare call propagates. Do not report it as swallowed, and do not ask for capture-and-re-raise. A Try name prefix does not establish [TryFunction], so resolve the declaration.
  • ignored-tryfunction-return-disables-try-semantics.md: the Anti Pattern now requires code that visibly expects the failure to be caught: a later GetLastErrorText/GetLastErrorCode/GetLastErrorObject check, a failure branch or result, or a loop meant to continue past failures. A bare call without such handling is explicitly not this anti-pattern.
    • .bad.al now shows the mistake in code: a bare call followed by a GetLastErrorText check that can never see the error.
    • .good.al adds an intentional bare call as the clean control.
  • al-error-handling-review.md: worklists both articles for bare [TryFunction] calls, and requires the same evidence before flagging.
  • Wording fixes in the events and performance articles listed above.
  • evaluation/review-fixtures.json: adds ignored-tryfunction-return-disables-try-semantics to the error-handling override. It also adds avoid-raising-events-inside-try-functions and use-tryfunction-for-error-catching-not-rollback, because editing their text triggers the changed-article coverage gate.

Evidence

BC versions: the new article is [all], because this is long-standing platform behaviour. The existing article keeps [13..].

Domain: error-handling. The concern is how errors move through a try-method call site, and that domain already owns the paired rule.

Not addressed here

The Security-domain comment (r3958572924) came from a leaf agent finding. Leaf retrieval is domain-scoped, so the Security leaf never loads this error-handling article. al-code-review.md also exempts leaf agent findings from the cross-leaf contradiction check, and Validate-FindingsReport.ps1 requires every leaf finding to appear in the rollup (SUPER_FINDING_MISSING). Suppressing such a finding needs a contract and validator change. I'd propose that separately rather than widen this PR.

Validation

  • Test-ReviewFixtures.ps1 with the changed-paths file: passed, 234 cases.
  • Test-ReviewContract.ps1: passed.
  • Test-KnowledgeRetrieval.ps1: passed, 403 articles and 691 samples round-tripped.
  • Build-KnowledgeIndex.ps1 / Build-SkillIndex.ps1: built cleanly.
  • validate_frontmatter.py was not run locally (no Python on this machine). I checked its rules by hand: six frontmatter keys, ## Description, no fenced code, under 100 lines, sample links present.
  • No model-based evaluation run.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed draft head da5a28819eb26dc786ff364ed74f0f85cb5aa940. The Error Handling correction is accurate, and the new good/bad controls correctly distinguish intended propagation from a dead last-error branch. One tightly coupled cross-domain contradiction remains in the events article this PR also edits.

microsoft/knowledge/events/avoid-raising-events-inside-try-functions.md:18,24 still unconditionally says raising an event inside a TryFunction body is an anti-pattern and that subscriber errors are caught/discarded. If an attributed publisher helper is invoked as a bare TryPublish();, a subscriber error propagates normally: no try boundary is activated. Qualifying only the opening Description sentence does not fix the normative Best Practice/Anti Pattern sections; al-events-review explicitly evaluates those sections and emits a major finding for a match, independently of the new Error Handling clarification.

Please qualify those normative sections by the actual consumed-result try invocation, explicitly exempt the intentionally bare publisher path, and include that path as a clean control. This requires no composed-review contract expansion or generic leaf-agent suppression change. Exact-head local validators pass; the gap is semantic rather than structural.

@JesperSchulz

Copy link
Copy Markdown
Contributor

Kilian Seizinger (@pri-kise), let's complete this one 😊 Anything you need from me?

…rror

The review agent repeatedly flagged bare calls to [TryFunction] procedures
(e.g. the System Application "Xml Validation" Try* APIs) as defects,
including claims that the failure is "silently swallowed". A bare call is
an ordinary call: the error propagates as usual.

- Add negative knowledge bare-tryfunction-call-propagates-errors.md.
- Narrow ignored-tryfunction-return-disables-try-semantics to code that
  visibly expects the failure to be caught; the bad sample now shows that
  in code, and the good sample includes an intentional bare call as the
  clean control.
- Qualify "TryFunction catches all errors" in the events article and the
  "must be consumed" wording in the performance article.
- Error-handling leaf worklists both articles for bare [TryFunction] calls
  and requires the same evidence before flagging.
- Add the three paired articles to the evaluation overrides.
… call sites

Address review on microsoft#211: the Best Practice and Anti Pattern of
avoid-raising-events-inside-try-functions now apply only when a call site
consumes the TryFunction's Boolean result. A publisher helper that is only
invoked as a bare statement activates no try boundary, so subscriber errors
propagate; it is explicitly exempt and the good sample adds it as a clean
control.
@pri-kise
Kilian Seizinger (pri-kise) force-pushed the prevent-try-function-wrong-warning branch from da5a288 to 609ef4d Compare November 17, 2026 09:53
@pri-kise

Copy link
Copy Markdown
Contributor Author

Thanks Jesper Schulz-Wedde (@JesperSchulz)! Rebased onto current main and addressed the events article in 609ef4d:

  • Title, Best Practice and Anti Pattern of avoid-raising-events-inside-try-functions now apply only to call sites that consume the Boolean result (Success := Try…() / if not Try…() then); the anti pattern asks to resolve each call site before flagging.
  • A publisher helper invoked only as a bare statement is explicitly exempt and points to bare-tryfunction-call-propagates-errors.
  • The good sample adds ConfirmPayment → bare TryPublishPaymentConfirmed() raising OnAfterConfirmPayment as the clean control.

Local validators pass (Test-ReviewFixtures 313 cases, Test-ReviewContract, Test-KnowledgeRetrieval 442 articles, Test-ReviewFixtureContexts). Ready for another look.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this resolves the remaining issue. The events guidance now distinguishes calls that catch errors from bare calls that intentionally let them propagate, and the new example covers the bare publisher case.

@JesperSchulz
Jesper Schulz-Wedde (JesperSchulz) merged commit 90540bb into microsoft:main Nov 17, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants