Skip to content

Fix/expunge historical version exception message #6880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

patrick-werner
Copy link
Member

Summary

Improve exception handling in the asynchronous Expunge job so that missing historical versions throw a clear, contextual error instead of HAPI‑2223: null.

Changes

In JpaResourceExpungeService.expungeHistoricalVersion(), replace orElseThrow(IllegalArgumentException::new) with a lambda that includes the missing version’s ID and partition in the exception message.

Update PartitionRunner’s error‑handling to extract cause.getMessage() and propagate it, ensuring the logged and thrown InternalErrorException carries the real root‑cause text.

Add a simple unit test to verify that an absent history entry yields a non‑null, descriptive exception message.

Related issue
Closes #6879

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

hapi-fhir-storage/src/main/java/ca/uhn/fhir/jpa/dao/expunge/PartitionRunner.java:130

  • [nitpick] Consider adding a separator (e.g., ': ') between Msg.code(1085) and cause.getMessage() to improve the clarity of the exception message.
throw new InternalErrorException(Msg.code(1085) + cause.getMessage(), cause);

@robogary
Copy link
Contributor

Formatting check succeeded!

@robogary
Copy link
Contributor

robogary commented May 6, 2025

Formatting check succeeded!

myResourceHistoryTableDao.findById(theNextVersionId).orElseThrow(IllegalArgumentException::new);
ResourceHistoryTable version = myResourceHistoryTableDao
.findById(theNextVersionId)
.orElseThrow(() -> new IllegalArgumentException(MessageFormat.format(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess we should probably include an error code here - We try to include them on all newly thrown exception messages that might end up visible to an end user. (FYI we use this page to reserve them)

Suggested change
.orElseThrow(() -> new IllegalArgumentException(MessageFormat.format(
.orElseThrow(() -> new IllegalArgumentException(Msg.code(2701) + MessageFormat.format(

ResourceHistoryTablePk pk = new ResourceHistoryTablePk();
pk.setPartitionIdValue(42);
// set versionId via reflection to avoid null in message
Field versionIdField = ResourceHistoryTablePk.class.getDeclaredField("myVersionId");
Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO we could just add a setter to this field so the test has better type safety... No harm in adding it, it's just not there 'cause it wasn't needed before

when(myResourceHistoryTableDao.findById(pk)).thenReturn(Optional.empty());

AtomicInteger remaining = new AtomicInteger(1);
IllegalArgumentException ex = assertThrows(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nitpick: This is fine, but @fil512 just pointed out to me how much nicer the assertThatThrownBy syntax can be for this kind of assertion the other day:

assertThatThrownBy(() -> myInterceptorService.runWithFilterHooks(Pointcut.TEST_FILTER, myParams, mySupplier))
.hasMessageContaining("Supplier was not executed in filter")
.hasMessageContaining("InterceptorServiceTest$FilterHooks$1.testFilter");

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.

Improve expungeHistoricalVersion exception to include context (avoid HAPI-2223: null)
3 participants