Skip to content

Commit

Permalink
Throw better exceptions when connecting to non-existent chroma server
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeWithKyrian committed Mar 4, 2024
1 parent 1133960 commit b27ad8b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Generated/ChromaApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Codewithkyrian\ChromaDB\Generated;

use Codewithkyrian\ChromaDB\Generated\Exceptions\ChromaConnectionException;
use Codewithkyrian\ChromaDB\Generated\Exceptions\ChromaException;
use Codewithkyrian\ChromaDB\Generated\Models\Collection;
use Codewithkyrian\ChromaDB\Generated\Models\Database;
Expand All @@ -21,6 +22,7 @@
use Codewithkyrian\ChromaDB\Generated\Responses\GetItemsResponse;
use Codewithkyrian\ChromaDB\Generated\Responses\QueryItemsResponse;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Client\ClientExceptionInterface;

Expand Down Expand Up @@ -318,6 +320,13 @@ public function reset(): bool

private function handleChromaApiException(\Exception|ClientExceptionInterface $e): void
{
if ($e instanceof ConnectException) {
$context = $e->getHandlerContext();
$message = $context['error'] ?? $e->getMessage();
$code = $context['errno'] ?? $e->getCode();
throw new ChromaConnectionException($message, $code);
}

if ($e instanceof RequestException) {
$errorString = $e->getResponse()->getBody()->getContents();

Expand Down
11 changes: 11 additions & 0 deletions src/Generated/Exceptions/ChromaConnectionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Codewithkyrian\ChromaDB\Generated\Exceptions;


class ChromaConnectionException extends ChromaException
{

}
8 changes: 8 additions & 0 deletions tests/ChromaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Codewithkyrian\ChromaDB\Client;
use Codewithkyrian\ChromaDB\ChromaDB;
use Codewithkyrian\ChromaDB\Generated\Exceptions\ChromaAuthorizationException;
use Codewithkyrian\ChromaDB\Generated\Exceptions\ChromaConnectionException;

it('can connect to a normal chroma server', function () {
$client = ChromaDB::client();
Expand Down Expand Up @@ -42,3 +43,10 @@
->withPort(8001)
->connect();
})->throws(ChromaAuthorizationException::class);

it('throws a connection exception when connecting to a non-existent chroma server', function () {
ChromaDB::factory()
->withHost('http://localhost')
->withPort(8002)
->connect();
})->throws(ChromaConnectionException::class);

0 comments on commit b27ad8b

Please sign in to comment.