Skip to content

add error message for content not allowed in trailing section#2658

Merged
rrayst merged 7 commits into
masterfrom
xslt-fix
Jan 23, 2026
Merged

add error message for content not allowed in trailing section#2658
rrayst merged 7 commits into
masterfrom
xslt-fix

Conversation

@christiangoerdes

@christiangoerdes christiangoerdes commented Jan 22, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Bug Fixes

    • Clearer, production-safe messages for malformed XML (prolog/trailing content); exchanges now abort on those parse errors.
    • More consistent error handling across expression evaluations, JSONPath and SpEL, reducing noisy internal details.
  • New Features

    • Option to suppress inclusion of internal exception objects from problem reports.
    • New utilities to truncate or take the tail of long text; improved string representation of problem reports.
  • Tests

    • Added unit tests for text utilities and XML error handling.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Jan 22, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Adds targeted XML parsing detection for prolog/trailing-section Transformer/XPath errors, enriches ExchangeExpressionException with contextual messages and fluent metadata (body/extension/exclude), changes flow invocation to abort on expression failures, adds StringUtil truncate/tail helpers and tests, and implements ProblemDetails.toString().

Changes

Cohort / File(s) Summary
Exception handling core
core/src/main/java/com/predic8/membrane/core/lang/ExchangeExpressionException.java
New includeException flag, new constructor ExchangeExpressionException(String expression, Throwable cause, String message), fluent methods body(...), extension(...), and excludeException(), and conditional inclusion of the exception in ProblemDetails.
XPath / XML parsing
core/src/main/java/com/predic8/membrane/core/lang/xpath/XPathExchangeExpression.java, core/src/main/java/com/predic8/membrane/core/interceptor/xslt/XSLTInterceptor.java
Detects "not allowed in prolog" and "is not allowed in trailing section" messages in nested causes, creates ExchangeExpressionException with contextual detail and truncated/tail body excerpts; XSLT branch aborts exchange for trailing-section case.
JSONPath / SpEL usage
core/src/main/java/com/predic8/membrane/core/lang/jsonpath/JsonpathExchangeExpression.java, core/src/main/java/com/predic8/membrane/core/lang/spel/SpELExchangeExpression.java
Standardized error construction using new ExchangeExpressionException constructor; attach body/extension/context and call .excludeException() for user-facing errors; preserve optional type info for SpEL converter errors.
Flow control / invocation
core/src/main/java/com/predic8/membrane/core/interceptor/flow/choice/InterceptorContainer.java
invokeFlow now returns ABORT on expression-evaluation errors instead of throwing ExchangeExpressionException.
ProblemDetails & logging
core/src/main/java/com/predic8/membrane/core/exceptions/ProblemDetails.java
Added toString() override that renders a full textual representation of ProblemDetails fields.
String utilities & tests
core/src/main/java/com/predic8/membrane/core/util/text/StringUtil.java, core/src/test/java/.../StringUtilTest.java, core/src/test/java/.../xpath/XPathExchangeExpressionTest.java
Added truncateAfter and tail helpers and tests; added XPath tests for prolog/trailing error scenarios and helper to extract ProblemDetails from exceptions.

Sequence Diagrams

sequenceDiagram
    participant Client
    participant XPathEngine as XPath / XSLT Engine
    participant ExprEvaluator as ExchangeExpressionEvaluator
    participant FlowController as InterceptorContainer
    participant ProblemBuilder as ProblemDetails/Response

    Client->>ExprEvaluator: submit exchange with XML body
    ExprEvaluator->>XPathEngine: parse/evaluate XPath/XSLT
    XPathEngine-->>ExprEvaluator: throws Transformer/XPath exception
    ExprEvaluator->>ExprEvaluator: inspect nested cause messages
    alt "not allowed in prolog" or "is not allowed in trailing section"
        ExprEvaluator->>ProblemBuilder: build ExchangeExpressionException(detail + truncated/tail body)
        ExprEvaluator->>FlowController: return ABORT
        FlowController->>ProblemBuilder: assemble production-safe ProblemDetails (respect excludeException)
        ProblemBuilder-->>Client: send error response
    else other error
        ExprEvaluator->>ProblemBuilder: create standard ExchangeExpressionException
        ExprEvaluator->>FlowController: propagate per existing flow (may createErrorResponse)
        FlowController->>ProblemBuilder: assemble response
        ProblemBuilder-->>Client: send error response
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

7.x

Suggested reviewers

  • rrayst

Poem

🐰 I found stray crumbs in XML's lair,
I trimmed the tail and showed them care.
With hops and snips the message sings,
Now errors wear kinder, shorter wings. ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.81% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'add error message for content not allowed in trailing section' directly aligns with the main changes in the XSLTInterceptor and XPathExchangeExpression files, which add handling for the 'is not allowed in trailing section' error case.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@membrane-ci-server

Copy link
Copy Markdown

This pull request needs "/ok-to-test" from an authorized committer.

@christiangoerdes

Copy link
Copy Markdown
Collaborator Author

/ok-to-test

…SONPath, and SpEL expressions

- Centralized handling of exceptions in XPath and JSONPath expressions.
- Enhanced error messages for unmapped XML prefixes and invalid JSONPath exceptions.
- Introduced `tail` method in `StringUtil` for handling detailed error outputs.
- Improved test coverage for corner cases in expression evaluations.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In
`@core/src/main/java/com/predic8/membrane/core/lang/jsonpath/JsonpathExchangeExpression.java`:
- Around line 101-107: The getErrorMessageForJsonPath method in
JsonpathExchangeExpression currently embeds request body content into the
exception message; change it to return a generic error message (e.g., "Error
evaluating Jsonpath %s.") instead of including the body and move the body
content into the ExchangeExpressionException body for internal diagnostics (use
the same pattern as XPathExchangeExpression). Specifically, update
getErrorMessageForJsonPath to avoid including truncateAfter(body, 200) in the
returned string and ensure wherever ExchangeExpressionException is created for
JsonpathExchangeExpression you call .body(body) (or equivalent) on the exception
so internal logs have the body but clients only see the generic message.
🧹 Nitpick comments (4)
core/src/main/java/com/predic8/membrane/core/util/text/StringUtil.java (1)

29-43: Consider adding validation for negative maxLength values.

Both truncateAfter and tail methods don't handle negative maxLength inputs. truncateAfter would throw StringIndexOutOfBoundsException if maxLength < 0, and tail would return the full string (unintended behavior). If defensive validation is desired:

♻️ Optional: Add parameter validation
 public static String truncateAfter(String s, int maxLength) {
+    if (maxLength < 0) throw new IllegalArgumentException("maxLength must be non-negative");
     return s.substring(0, min(s.length(), maxLength));
 }

 public static String tail(String s, int maxLength) {
+    if (maxLength < 0) throw new IllegalArgumentException("maxLength must be non-negative");
     return s.substring(Math.max(s.length() - maxLength,0));
 }
core/src/test/java/com/predic8/membrane/core/util/text/StringUtilTest.java (1)

10-15: Consider expanding test coverage.

The test covers basic tail scenarios but is missing:

  • Tests for truncateAfter method (also newly added)
  • Edge case: maxLength = 0
♻️ Suggested additional tests
 `@Test`
 void testTail() {
     assertEquals("def", tail("abcdef",3));
     assertEquals("abcdef", tail("abcdef",10));
     assertEquals("", tail("",10));
+    assertEquals("", tail("abcdef", 0));
+}
+
+@Test
+void testTruncateAfter() {
+    assertEquals("abc", truncateAfter("abcdef", 3));
+    assertEquals("abcdef", truncateAfter("abcdef", 10));
+    assertEquals("", truncateAfter("", 10));
+    assertEquals("", truncateAfter("abcdef", 0));
 }

Don't forget to add the import:

import static com.predic8.membrane.core.util.text.StringUtil.truncateAfter;
core/src/main/java/com/predic8/membrane/core/lang/xpath/XPathExchangeExpression.java (1)

68-68: Consider a more professional error message.

The message "Should not Happen!" is informal for production code. A more descriptive message would aid debugging.

♻️ Suggested fix
-            throw new RuntimeException("Should not Happen!");
+            throw new RuntimeException("Unsupported evaluation type: " + type.getName());
core/src/main/java/com/predic8/membrane/core/lang/jsonpath/JsonpathExchangeExpression.java (1)

85-98: Include the throwable in logs to preserve stack traces.

Right now only the formatted message is logged, which drops the exception context needed for debugging.

♻️ Suggested tweak (preserve stack traces)
-            log.error(msg);
+            log.error(msg, ipe);
@@
-            log.info(msg);
+            log.info(msg, e);
@@
-            log.info(msg);
+            log.info(msg, e);

@predic8 predic8 requested a review from rrayst January 23, 2026 09:32
@rrayst rrayst merged commit 81a5736 into master Jan 23, 2026
5 checks passed
@rrayst rrayst deleted the xslt-fix branch January 23, 2026 15:25
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.

3 participants