Skip to content

Commit 88d851c

Browse files
Handles resource idnex not present scenario gracefully
Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
1 parent 9fe5b08 commit 88d851c

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/main/java/org/opensearch/security/resources/api/list/AccessibleResourcesRestAction.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.IOException;
1212
import java.util.List;
1313
import java.util.Objects;
14+
import java.util.Set;
1415

1516
import com.google.common.collect.ImmutableList;
1617
import org.apache.logging.log4j.LogManager;
@@ -66,21 +67,27 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
6667

6768
final String resourceIndex = resourcePluginInfo.indexByType(resourceType);
6869

70+
if (resourceIndex == null) {
71+
return channel -> { handleResponse(channel, Set.of()); };
72+
}
6973
return channel -> resourceAccessHandler.getResourceSharingInfoForCurrentUser(resourceIndex, ActionListener.wrap(rows -> {
74+
handleResponse(channel, rows);
75+
}, e -> handleError(channel, e)));
76+
}
7077

71-
try (XContentBuilder b = channel.newBuilder()) {
72-
b.startObject();
73-
b.startArray("resources");
74-
for (SharingRecord row : rows) {
75-
row.toXContent(b, ToXContent.EMPTY_PARAMS);
76-
}
77-
b.endArray();
78-
b.endObject();
79-
channel.sendResponse(new BytesRestResponse(RestStatus.OK, b));
80-
} catch (IOException ioe) {
81-
handleError(channel, ioe);
78+
private void handleResponse(RestChannel channel, Set<SharingRecord> records) throws IOException {
79+
try (XContentBuilder b = channel.newBuilder()) {
80+
b.startObject();
81+
b.startArray("resources");
82+
for (SharingRecord row : records) {
83+
row.toXContent(b, ToXContent.EMPTY_PARAMS);
8284
}
83-
}, e -> handleError(channel, e)));
85+
b.endArray();
86+
b.endObject();
87+
channel.sendResponse(new BytesRestResponse(RestStatus.OK, b));
88+
} catch (IOException ioe) {
89+
handleError(channel, ioe);
90+
}
8491
}
8592

8693
private void handleError(RestChannel channel, Exception e) {

0 commit comments

Comments
 (0)