Skip to content

Commit 4bf1435

Browse files
committed
Fix NPE in ElasticsearchExceptionTranslator.
Original Pull Request #2389 Closes #2388 (cherry picked from commit 9446d72)
1 parent 8fe0417 commit 4bf1435

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchExceptionTranslator.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,24 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
7474
if (ex instanceof ElasticsearchException elasticsearchException) {
7575

7676
ErrorResponse response = elasticsearchException.response();
77+
var errorType = response.error().type();
78+
var errorReason = response.error().reason() != null ? response.error().reason() : "undefined reason";
7779

78-
if (response.status() == 404 && "index_not_found_exception".equals(response.error().type())) {
80+
if (response.status() == 404 && "index_not_found_exception".equals(errorType)) {
7981

8082
// noinspection RegExpRedundantEscape
8183
Pattern pattern = Pattern.compile(".*no such index \\[(.*)\\]");
8284
String index = "";
83-
Matcher matcher = pattern.matcher(response.error().reason());
85+
Matcher matcher = pattern.matcher(errorReason);
8486
if (matcher.matches()) {
8587
index = matcher.group(1);
8688
}
8789
return new NoSuchIndexException(index);
8890
}
8991
String body = JsonUtils.toJson(response, jsonpMapper);
9092

91-
if (response.error().type().contains("validation_exception")) {
92-
return new DataIntegrityViolationException(response.error().reason());
93+
if (errorType != null && errorType.contains("validation_exception")) {
94+
return new DataIntegrityViolationException(errorReason);
9395
}
9496

9597
return new UncategorizedElasticsearchException(ex.getMessage(), response.status(), body, ex);

0 commit comments

Comments
 (0)