2828import com .fasterxml .jackson .core .JsonProcessingException ;
2929import com .fasterxml .jackson .databind .ObjectMapper ;
3030
31+ import org .slf4j .Logger ;
32+ import org .slf4j .LoggerFactory ;
33+
3134import org .springframework .ai .chroma .vectorstore .ChromaApi .QueryRequest .Include ;
3235import org .springframework .ai .chroma .vectorstore .common .ChromaApiConstants ;
3336import org .springframework .ai .util .json .JsonParser ;
3437import org .springframework .http .HttpHeaders ;
38+ import org .springframework .http .HttpStatus ;
3539import org .springframework .http .MediaType ;
3640import org .springframework .http .client .support .BasicAuthenticationInterceptor ;
3741import org .springframework .lang .Nullable ;
5256 */
5357public class ChromaApi {
5458
59+ private static final Logger logger = LoggerFactory .getLogger (ChromaApi .class );
60+
5561 public static Builder builder () {
5662 return new Builder ();
5763 }
@@ -136,12 +142,15 @@ public Tenant getTenant(String tenantName) {
136142 .retrieve ()
137143 .body (Tenant .class );
138144 }
139- catch (HttpServerErrorException | HttpClientErrorException e ) {
140- String msg = this . getErrorMessage ( e );
141- if ( String . format ("Tenant [%s] not found " , tenantName ). equals ( msg )) {
145+ catch (HttpClientErrorException e ) {
146+ if ( e . getStatusCode () == HttpStatus . NOT_FOUND ) {
147+ logger . debug ("Tenant [{}] does not exist, returning null " , tenantName );
142148 return null ;
143149 }
144- throw new RuntimeException (msg , e );
150+ throw new RuntimeException (this .getErrorMessage (e ), e );
151+ }
152+ catch (HttpServerErrorException e ) {
153+ throw new RuntimeException (this .getErrorMessage (e ), e );
145154 }
146155 }
147156
@@ -165,12 +174,15 @@ public Database getDatabase(String tenantName, String databaseName) {
165174 .retrieve ()
166175 .body (Database .class );
167176 }
168- catch (HttpServerErrorException | HttpClientErrorException e ) {
169- String msg = this . getErrorMessage ( e );
170- if ( msg . startsWith ( String . format ( "Database [%s] not found. " , databaseName ))) {
177+ catch (HttpClientErrorException e ) {
178+ if ( e . getStatusCode () == HttpStatus . NOT_FOUND ) {
179+ logger . debug ( "Database [{}] in tenant [{}] does not exist, returning null " , databaseName , tenantName );
171180 return null ;
172181 }
173- throw new RuntimeException (msg , e );
182+ throw new RuntimeException (this .getErrorMessage (e ), e );
183+ }
184+ catch (HttpServerErrorException e ) {
185+ throw new RuntimeException (this .getErrorMessage (e ), e );
174186 }
175187 }
176188
@@ -226,12 +238,16 @@ public Collection getCollection(String tenantName, String databaseName, String c
226238 .retrieve ()
227239 .body (Collection .class );
228240 }
229- catch (HttpServerErrorException | HttpClientErrorException e ) {
230- String msg = this .getErrorMessage (e );
231- if (String .format ("Collection [%s] does not exists" , collectionName ).equals (msg )) {
241+ catch (HttpClientErrorException e ) {
242+ if (e .getStatusCode () == HttpStatus .NOT_FOUND ) {
243+ logger .debug ("Collection [{}] in database [{}] and tenant [{}] does not exist, returning null" ,
244+ collectionName , databaseName , tenantName );
232245 return null ;
233246 }
234- throw new RuntimeException (msg , e );
247+ throw new RuntimeException (this .getErrorMessage (e ), e );
248+ }
249+ catch (HttpServerErrorException e ) {
250+ throw new RuntimeException (this .getErrorMessage (e ), e );
235251 }
236252 }
237253
0 commit comments