31
31
import org .springframework .http .client .support .BasicAuthenticationInterceptor ;
32
32
import org .springframework .util .CollectionUtils ;
33
33
import org .springframework .util .StringUtils ;
34
+ import org .springframework .web .client .HttpClientErrorException ;
34
35
import org .springframework .web .client .HttpServerErrorException ;
36
+ import org .springframework .web .client .HttpStatusCodeException ;
35
37
import org .springframework .web .client .RestClient ;
36
38
37
39
import com .fasterxml .jackson .annotation .JsonProperty ;
@@ -49,6 +51,9 @@ public class ChromaApi {
49
51
// Regular expression pattern that looks for a message inside the ValueError(...).
50
52
private static Pattern VALUE_ERROR_PATTERN = Pattern .compile ("ValueError\\ ('([^']*)'\\ )" );
51
53
54
+ // Regular expression pattern that looks for a message.
55
+ private static Pattern MESSAGE_ERROR_PATTERN = Pattern .compile ("\" message\" :\" (.*?)\" " );
56
+
52
57
private RestClient restClient ;
53
58
54
59
private final ObjectMapper objectMapper ;
@@ -316,8 +321,8 @@ public Collection getCollection(String collectionName) {
316
321
.toEntity (Collection .class )
317
322
.getBody ();
318
323
}
319
- catch (HttpServerErrorException e ) {
320
- String msg = this .getValueErrorMessage ( e . getMessage () );
324
+ catch (HttpServerErrorException | HttpClientErrorException e ) {
325
+ String msg = this .getErrorMessage ( e );
321
326
if (String .format ("Collection %s does not exist." , collectionName ).equals (msg )) {
322
327
return null ;
323
328
}
@@ -413,12 +418,28 @@ private void httpHeaders(HttpHeaders headers) {
413
418
}
414
419
}
415
420
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 )) {
418
426
return "" ;
419
427
}
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 "" ;
422
443
}
423
444
424
445
}
0 commit comments