Skip to content

Commit b27ad8b

Browse files
Throw better exceptions when connecting to non-existent chroma server
1 parent 1133960 commit b27ad8b

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/Generated/ChromaApiClient.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace Codewithkyrian\ChromaDB\Generated;
77

8+
use Codewithkyrian\ChromaDB\Generated\Exceptions\ChromaConnectionException;
89
use Codewithkyrian\ChromaDB\Generated\Exceptions\ChromaException;
910
use Codewithkyrian\ChromaDB\Generated\Models\Collection;
1011
use Codewithkyrian\ChromaDB\Generated\Models\Database;
@@ -21,6 +22,7 @@
2122
use Codewithkyrian\ChromaDB\Generated\Responses\GetItemsResponse;
2223
use Codewithkyrian\ChromaDB\Generated\Responses\QueryItemsResponse;
2324
use GuzzleHttp\Client;
25+
use GuzzleHttp\Exception\ConnectException;
2426
use GuzzleHttp\Exception\RequestException;
2527
use Psr\Http\Client\ClientExceptionInterface;
2628

@@ -318,6 +320,13 @@ public function reset(): bool
318320

319321
private function handleChromaApiException(\Exception|ClientExceptionInterface $e): void
320322
{
323+
if ($e instanceof ConnectException) {
324+
$context = $e->getHandlerContext();
325+
$message = $context['error'] ?? $e->getMessage();
326+
$code = $context['errno'] ?? $e->getCode();
327+
throw new ChromaConnectionException($message, $code);
328+
}
329+
321330
if ($e instanceof RequestException) {
322331
$errorString = $e->getResponse()->getBody()->getContents();
323332

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codewithkyrian\ChromaDB\Generated\Exceptions;
6+
7+
8+
class ChromaConnectionException extends ChromaException
9+
{
10+
11+
}

tests/ChromaDB.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Codewithkyrian\ChromaDB\Client;
66
use Codewithkyrian\ChromaDB\ChromaDB;
77
use Codewithkyrian\ChromaDB\Generated\Exceptions\ChromaAuthorizationException;
8+
use Codewithkyrian\ChromaDB\Generated\Exceptions\ChromaConnectionException;
89

910
it('can connect to a normal chroma server', function () {
1011
$client = ChromaDB::client();
@@ -42,3 +43,10 @@
4243
->withPort(8001)
4344
->connect();
4445
})->throws(ChromaAuthorizationException::class);
46+
47+
it('throws a connection exception when connecting to a non-existent chroma server', function () {
48+
ChromaDB::factory()
49+
->withHost('http://localhost')
50+
->withPort(8002)
51+
->connect();
52+
})->throws(ChromaConnectionException::class);

0 commit comments

Comments
 (0)