Skip to content

Handle metadata fetching errors gracefully#189

Open
antoinebhs wants to merge 3 commits into
mainfrom
handle-metadata-error
Open

Handle metadata fetching errors gracefully#189
antoinebhs wants to merge 3 commits into
mainfrom
handle-metadata-error

Conversation

@antoinebhs
Copy link
Copy Markdown
Contributor

PR Summary

Handle metadata-service connectivity failures by catching ResourceAccessException in IDirectoryElementsService, logging the affected element type, and returning the elements with empty specificMetadata instead of failing the whole request.
This makes it possible to display elements in Gridexplore without having the metadata server up (like actions-server).

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 22, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 79810c4b-cdb3-45c6-a225-4bbfce674d54

📥 Commits

Reviewing files that changed from the base of the PR and between 9b2df50 and 4e60f4c.

📒 Files selected for processing (1)
  • src/test/java/org/gridsuite/explore/server/ExploreTest.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/test/java/org/gridsuite/explore/server/ExploreTest.java

📝 Walkthrough

Walkthrough

When the metadata service is unreachable, IDirectoryElementsService.completeElementAttribute catches ResourceAccessException, logs a warning, and returns elements with empty specificMetadata. ExploreTest simulates the failure and asserts the metadata endpoint returns a partial response with the expected element.

Changes

Graceful metadata loading failure

Layer / File(s) Summary
Error handling and logging in metadata completion
src/main/java/org/gridsuite/explore/server/services/IDirectoryElementsService.java
Imports SLF4J logging and ResourceAccessException; defines interface-level LOGGER; wraps completeElementAttribute metadata-fetching in try/catch to log warnings and return input elements with empty specificMetadata on exception.
Test setup and dispatcher for failure simulation
src/test/java/org/gridsuite/explore/server/ExploreTest.java
Adds MockWebServer3 import; introduces UUID constant for failing element; serializes its ElementAttributes in @BeforeEach; extends MockWebServer Dispatcher to return the element while simulating a contingency-list metadata failure via SocketPolicy.DisconnectAfterRequest.
Partial response test on metadata failure
src/test/java/org/gridsuite/explore/server/ExploreTest.java
New JUnit test verifies /v1/explore/elements/metadata returns one element with matching UUID/name and an empty specificMetadata when contingency-list metadata loading fails.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: handling metadata fetching errors gracefully, which is the primary objective of the pull request.
Description check ✅ Passed The description directly relates to the changeset, explaining the error handling mechanism, logging behavior, and the business purpose of allowing elements to display without the metadata server.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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


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.

# Conflicts:
#	src/test/java/org/gridsuite/explore/server/ExploreTest.java
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

@TheMaskedTurtle TheMaskedTurtle left a comment

Choose a reason for hiding this comment

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

Nice ! :)

return lstElementAttribute.stream()
.map(elementAttributes -> populateMedataItem(elementAttributes, Map.of()))
.collect(Collectors.toList());
}
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.

Suggested change
}
List<Map<String, Object>> metadata = safeGetMetadata(lstElementAttribute);
return metadata.stream().map(metadataItem -> {
Object item = metadataItem.get("id");
if (item == null) {
item = metadataItem.getOrDefault("uuid", "");
}
ElementAttributes e = mapElementAttribute.get(item.toString());
return populateMedataItem(e, metadataItem);
}).collect(Collectors.toList());
}
private List<Map<String, Object>> safeGetMetadata(List<ElementAttributes> lstElementAttribute) {
List<Map<String, Object>> metadata;
try {
metadata = getMetadata(lstElementAttribute.stream().map(ElementAttributes::getElementUuid).collect(Collectors.toList()));
} catch (ResourceAccessException e) {
String elementType = lstElementAttribute.isEmpty() ? "UNKNOWN" : lstElementAttribute.getFirst().getType();
LOGGER.warn("{} metadata service is unavailable, returning elements with empty specific metadata", elementType);
metadata = Collections.nCopies(lstElementAttribute.size(), Map.of());
}
return metadata;
}

Refactoring suggestion + maybe no need to log the error stack in the warn as we identify the type already, and that we enter this case only on ResourceAccessException

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