Skip to content

Commit 8354507

Browse files
ricken07markpollack
authored andcommitted
handle http client error exception on getCollection method
1 parent 2ecffc1 commit 8354507

File tree

1 file changed

+27
-6
lines changed
  • vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma

1 file changed

+27
-6
lines changed

vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/ChromaApi.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
3232
import org.springframework.util.CollectionUtils;
3333
import org.springframework.util.StringUtils;
34+
import org.springframework.web.client.HttpClientErrorException;
3435
import org.springframework.web.client.HttpServerErrorException;
36+
import org.springframework.web.client.HttpStatusCodeException;
3537
import org.springframework.web.client.RestClient;
3638

3739
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -49,6 +51,9 @@ public class ChromaApi {
4951
// Regular expression pattern that looks for a message inside the ValueError(...).
5052
private static Pattern VALUE_ERROR_PATTERN = Pattern.compile("ValueError\\('([^']*)'\\)");
5153

54+
// Regular expression pattern that looks for a message.
55+
private static Pattern MESSAGE_ERROR_PATTERN = Pattern.compile("\"message\":\"(.*?)\"");
56+
5257
private RestClient restClient;
5358

5459
private final ObjectMapper objectMapper;
@@ -316,8 +321,8 @@ public Collection getCollection(String collectionName) {
316321
.toEntity(Collection.class)
317322
.getBody();
318323
}
319-
catch (HttpServerErrorException e) {
320-
String msg = this.getValueErrorMessage(e.getMessage());
324+
catch (HttpServerErrorException | HttpClientErrorException e) {
325+
String msg = this.getErrorMessage(e);
321326
if (String.format("Collection %s does not exist.", collectionName).equals(msg)) {
322327
return null;
323328
}
@@ -413,12 +418,28 @@ private void httpHeaders(HttpHeaders headers) {
413418
}
414419
}
415420

416-
private String getValueErrorMessage(String logString) {
417-
if (!StringUtils.hasText(logString)) {
421+
private String getErrorMessage(HttpStatusCodeException e) {
422+
var errorMessage = e.getMessage();
423+
424+
// If the error message is empty or null, return an empty string
425+
if (!StringUtils.hasText(errorMessage)) {
418426
return "";
419427
}
420-
Matcher m = VALUE_ERROR_PATTERN.matcher(logString);
421-
return (m.find()) ? m.group(1) : "";
428+
429+
// If the exception is an HttpServerErrorException, use the VALUE_ERROR_PATTERN
430+
Matcher valueErrorMatcher = VALUE_ERROR_PATTERN.matcher(errorMessage);
431+
if (e instanceof HttpServerErrorException && valueErrorMatcher.find()) {
432+
return valueErrorMatcher.group(1);
433+
}
434+
435+
// Otherwise, use the MESSAGE_ERROR_PATTERN for other cases
436+
Matcher messageErrorMatcher = MESSAGE_ERROR_PATTERN.matcher(errorMessage);
437+
if (messageErrorMatcher.find()) {
438+
return messageErrorMatcher.group(1);
439+
}
440+
441+
// If no pattern matches, return an empty string
442+
return "";
422443
}
423444

424445
}

0 commit comments

Comments
 (0)