Skip to content

Commit 4a647d2

Browse files
committed
better JSON exception handling in storages
1 parent 5f71c9b commit 4a647d2

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

Chunk.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ public function __construct(
2222
protected $id,
2323
protected $text,
2424
protected $embedding,
25-
$lang = '',
26-
$created = '',
25+
$lang = '',
26+
$created = '',
2727
protected $score = 0
28-
) {
28+
)
29+
{
2930
$this->language = $lang ?: $this->determineLanguage();
3031
$this->created = $created ?: time();
3132
}
@@ -34,7 +35,7 @@ public function __toString(): string
3435
{
3536
$string = $this->page . '#' . $this->id;
3637
if ($this->score) {
37-
$string .= ' (' . $this->score . ')';
38+
$string .= sprintf(' (%.2f)', $this->score);
3839
}
3940
return $string;
4041
}

Storage/ChromaStorage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ protected function runQuery($endpoint, mixed $data, $method = 'POST')
6868

6969
try {
7070
$result = json_decode((string)$response, true, 512, JSON_THROW_ON_ERROR);
71-
} catch (\Exception) {
72-
throw new \Exception('Chroma API returned invalid JSON. ' . $response);
71+
} catch (\Exception $e) {
72+
throw new \Exception('Chroma API returned invalid JSON. ' . $response, 0, $e);
7373
}
7474

7575
if ((int)$this->http->status !== 200) {

Storage/PineconeStorage.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ protected function runQuery($endpoint, mixed $data, $method = 'POST')
5353
throw new \Exception('Pinecone API returned no response. ' . $this->http->error);
5454
}
5555

56+
try {
5657
$result = json_decode((string)$response, true, 512, JSON_THROW_ON_ERROR);
57-
if ($result === null) {
58-
throw new \Exception('Pinecone API returned invalid JSON. ' . $response);
58+
} catch (\JsonException $e) {
59+
throw new \Exception('Pinecone API returned invalid JSON. ' . $response, 0, $e);
5960
}
6061

6162
if (isset($result['message'])) {

Storage/QdrantStorage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ protected function runQuery($endpoint, mixed $data, $method = 'POST')
6969

7070
try {
7171
$result = json_decode((string)$response, true, 512, JSON_THROW_ON_ERROR);
72-
} catch (\Exception) {
73-
throw new \Exception('Qdrant API returned invalid JSON. ' . $response);
72+
} catch (\Exception $e) {
73+
throw new \Exception('Qdrant API returned invalid JSON. ' . $response, 0, $e);
7474
}
7575

7676
if ((int)$this->http->status !== 200) {

0 commit comments

Comments
 (0)