Skip to content

Make the #367 parse-error fallback work for jars without PolicyParseException - #377

Merged
muditchaudhary merged 1 commit into
cedar-policy:mainfrom
muditchaudhary:ffi-parse-exception-fallback
Nov 16, 2026
Merged

muditchaudhary merged 1 commit into
cedar-policy:mainfrom
muditchaudhary:ffi-parse-exception-fallback

Conversation

@muditchaudhary

@muditchaudhary muditchaudhary commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Make the #367 parse-error fallback work for jars without PolicyParseException

Summary

#367 added PolicyParseException, which the native library constructs when PolicySet.parsePolicies fails to
parse policy text. It was meant to stay backward compatible: if building the richer exception failed, the code
fell back to the generic path:

    // Fall back to the generic path if any part of building the richer exception fails, so
    // a parse error is never silently turned into a different kind of failure.

That fallback never worked for the case it needed to cover.

The problem

When the jar has no PolicyParseException:

  1. build_parse_exception fails to look up the class, which leaves a NoClassDefFoundError pending in the JVM,
    and returns Err.
  2. throw_parse_errors falls back to throw_internal, as Surface structured parse diagnostics from PolicySet.parsePolicies #367 intended.
  3. throw_internal returns without throwing anything whenever an exception is already pending, so the
    NoClassDefFoundError is what reaches Java.

So instead of the InternalException it has always received, the caller of PolicySet.parsePolicies gets a
NoClassDefFoundError. That's an Error, so a catch (InternalException e) doesn't catch it, and the parse
error itself is lost.

The fix

A new clear_missing_class_error runs before the fallback calls throw_internal:

  • It takes the pending exception and clears it, since IsInstanceOf may not be called while one is pending.
  • If the exception is a NoClassDefFoundError, it stays cleared. throw_internal then throws
    InternalException("Internal JNI Error: <parse errors>"), the same class and message as before Surface structured parse diagnostics from PolicySet.parsePolicies #367.
  • Any other exception (e.g., OutOfMemoryError) is put back unchanged and propagates as before.

Jars that include PolicyParseException are unaffected and still get the full exception with Cedar's structured
diagnostics.

Behaviour for an older jar

Before #367 After #367 With this PR
Exception InternalException NoClassDefFoundError InternalException
Message Internal JNI Error: <errors> com/cedarpolicy/model/exception/PolicyParseException Internal JNI Error: <errors>

The text of <errors> comes from the Cedar version the native library was built with.

Also in this PR

  • The CHANGELOG.md sections on main now match the 4.11.0 release.

Testing

  • I reproduced the failure by running this build's classes, minus PolicyParseException*.class, in a separate JVM
    against the new native library. Without the fix, parsePolicies on invalid text throws
    java.lang.NoClassDefFoundError: com/cedarpolicy/model/exception/PolicyParseException. With the fix, it throws
    InternalException with message Internal error: Internal JNI Error: unexpected token \::``.
  • The existing PolicyParseDiagnosticsTests pass, so a current jar still gets PolicyParseException.
  • ./gradlew build, cargo test --all-features, cargo fmt --check and cargo clippy --all-features --all-targets all pass.

Also updates

  • Changelog
  • Bump MacOS version for Github actions as 14 is deprecated.

@muditchaudhary
muditchaudhary marked this pull request as ready for review November 9, 2026 19:07
Signed-off-by: Mudit Chaudhary <chmudit@amazon.com>
@muditchaudhary
muditchaudhary force-pushed the ffi-parse-exception-fallback branch from 4817211 to 4fca6cc Compare November 9, 2026 19:12
if env.exception_clear().is_err() {
return;
}
let missing_class = env

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.

We could be stronger than NoClassDefFoundError here and catch java/lang/LinkageError since that's the parent to that as well as the sibling NoSuchMethodError, but since we only expect NoClassDefFoundError, this should be fine imo.

/// leaves a `NoClassDefFoundError` pending. Clear it, so that such a jar gets the
/// `InternalException` it has always received for a parse error. Any other pending exception,
/// such as an `OutOfMemoryError`, is put back to propagate.
fn clear_missing_class_error(env: &mut JNIEnv<'_>) {

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.

Nit: should we add a unit test covering this fallback behavior?

Something like

  • Call env.find_class("com/cedarpolicy/DoesNotExist"), then clear_missing_class_error, and assert !exception_check().
  • Call throw_new("java/lang/IllegalStateException", ..), then clear_missing_class_error, and assert the exception is still pending and is the same class.

@muditchaudhary
muditchaudhary merged commit 900c6f6 into cedar-policy:main Nov 16, 2026
4 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.

3 participants