Skip to content

Commit 17bd350

Browse files
Fix undefined variable $e in failover loops for image, audio, transcription, embeddings, and reranking (#225)
1 parent 74d5ef4 commit 17bd350

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

src/PendingResponses/PendingAudioGeneration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public function generate(Lab|array|string|null $provider = null, ?string $model
7575
$provider ?? config('ai.default_for_audio'), $model
7676
);
7777

78+
$lastException = null;
79+
7880
foreach ($providers as $provider => $model) {
7981
$provider = Ai::fakeableAudioProvider($provider);
8082

@@ -85,13 +87,15 @@ public function generate(Lab|array|string|null $provider = null, ?string $model
8587
$this->text, $this->voice, $this->instructions, $model
8688
);
8789
} catch (FailoverableException $e) {
90+
$lastException = $e;
91+
8892
event(new ProviderFailedOver($provider, $model, $e));
8993

9094
continue;
9195
}
9296
}
9397

94-
throw $e;
98+
throw $lastException;
9599
}
96100

97101
/**

src/PendingResponses/PendingEmbeddingsGeneration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public function generate(Lab|array|string|null $provider = null, ?string $model
5858
$provider ?? config('ai.default_for_embeddings'), $model
5959
);
6060

61+
$lastException = null;
62+
6163
foreach ($providers as $provider => $model) {
6264
$provider = Ai::fakeableEmbeddingProvider($provider);
6365

@@ -75,13 +77,15 @@ public function generate(Lab|array|string|null $provider = null, ?string $model
7577
fn ($response) => $this->cacheEmbeddings($provider, $model, $dimensions, $response)
7678
);
7779
} catch (FailoverableException $e) {
80+
$lastException = $e;
81+
7882
event(new ProviderFailedOver($provider, $model, $e));
7983

8084
continue;
8185
}
8286
}
8387

84-
throw $e;
88+
throw $lastException;
8589
}
8690

8791
/**

src/PendingResponses/PendingImageGeneration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public function generate(Lab|array|string|null $provider = null, ?string $model
118118
$provider ?? config('ai.default_for_images'), $model
119119
);
120120

121+
$lastException = null;
122+
121123
foreach ($providers as $provider => $model) {
122124
$provider = Ai::fakeableImageProvider($provider);
123125

@@ -128,13 +130,15 @@ public function generate(Lab|array|string|null $provider = null, ?string $model
128130
$this->prompt, $this->attachments, $this->size, $this->quality, $model, $this->timeout
129131
);
130132
} catch (FailoverableException $e) {
133+
$lastException = $e;
134+
131135
event(new ProviderFailedOver($provider, $model, $e));
132136

133137
continue;
134138
}
135139
}
136140

137-
throw $e;
141+
throw $lastException;
138142
}
139143

140144
/**

src/PendingResponses/PendingReranking.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function rerank(string $query, Lab|array|string|null $provider = null, ?s
4444
$provider ?? config('ai.default_for_reranking'), $model
4545
);
4646

47+
$lastException = null;
48+
4749
foreach ($providers as $provider => $model) {
4850
$provider = Ai::fakeableRerankingProvider($provider);
4951

@@ -52,12 +54,14 @@ public function rerank(string $query, Lab|array|string|null $provider = null, ?s
5254
try {
5355
return $provider->rerank($this->documents, $query, $this->limit, $model);
5456
} catch (FailoverableException $e) {
57+
$lastException = $e;
58+
5559
event(new ProviderFailedOver($provider, $model, $e));
5660

5761
continue;
5862
}
5963
}
6064

61-
throw $e;
65+
throw $lastException;
6266
}
6367
}

src/PendingResponses/PendingTranscriptionGeneration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public function generate(Lab|array|string|null $provider = null, ?string $model
7171
$provider ?? config('ai.default_for_transcription'), $model
7272
);
7373

74+
$lastException = null;
75+
7476
foreach ($providers as $provider => $model) {
7577
$provider = Ai::fakeableTranscriptionProvider($provider);
7678

@@ -79,13 +81,15 @@ public function generate(Lab|array|string|null $provider = null, ?string $model
7981
try {
8082
return $provider->transcribe($this->audio, $this->language, $this->diarize, $model, $this->timeout);
8183
} catch (FailoverableException $e) {
84+
$lastException = $e;
85+
8286
event(new ProviderFailedOver($provider, $model, $e));
8387

8488
continue;
8589
}
8690
}
8791

88-
throw $e;
92+
throw $lastException;
8993
}
9094

9195
/**

0 commit comments

Comments
 (0)