Skip to content

Commit 235fc58

Browse files
authored
fix: make PendingRequest only catch RequestExceptions (#436)
1 parent 8367977 commit 235fc58

File tree

17 files changed

+35
-89
lines changed

17 files changed

+35
-89
lines changed

src/Concerns/HandlesRequestExceptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
trait HandlesRequestExceptions
1515
{
16-
public function handleRequestExceptions(string $model, Throwable $e): never
16+
public function handleRequestException(string $model, Throwable $e): never
1717
{
1818
// Keep already raised PrismException
1919
if ($e instanceof PrismException) {

src/Embeddings/PendingRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace Prism\Prism\Embeddings;
66

7+
use Illuminate\Http\Client\RequestException;
78
use Prism\Prism\Concerns\ConfiguresClient;
89
use Prism\Prism\Concerns\ConfiguresProviders;
910
use Prism\Prism\Concerns\HasProviderOptions;
1011
use Prism\Prism\Exceptions\PrismException;
11-
use Throwable;
1212

1313
class PendingRequest
1414
{
@@ -71,8 +71,8 @@ public function asEmbeddings(): Response
7171

7272
try {
7373
return $this->provider->embeddings($request);
74-
} catch (Throwable $e) {
75-
$this->provider->handleRequestExceptions($request->model(), $e);
74+
} catch (RequestException $e) {
75+
$this->provider->handleRequestException($request->model(), $e);
7676
}
7777
}
7878

src/Images/PendingRequest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Prism\Prism\Images;
66

77
use Illuminate\Contracts\View\View;
8+
use Illuminate\Http\Client\RequestException;
89
use Prism\Prism\Concerns\ConfiguresClient;
910
use Prism\Prism\Concerns\ConfiguresModels;
1011
use Prism\Prism\Concerns\ConfiguresProviders;
@@ -28,7 +29,13 @@ public function withPrompt(string|View $prompt): self
2829

2930
public function generate(): Response
3031
{
31-
return $this->provider->images($this->toRequest());
32+
$request = $this->toRequest();
33+
34+
try {
35+
return $this->provider->images($this->toRequest());
36+
} catch (RequestException $e) {
37+
$this->provider->handleRequestException($request->model(), $e);
38+
}
3239
}
3340

3441
public function toRequest(): Request

src/Providers/Anthropic/Anthropic.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use Prism\Prism\Structured\Response as StructuredResponse;
2323
use Prism\Prism\Text\Request as TextRequest;
2424
use Prism\Prism\Text\Response as TextResponse;
25-
use Throwable;
2625

2726
class Anthropic extends Provider
2827
{
@@ -73,16 +72,8 @@ public function stream(TextRequest $request): Generator
7372
return $handler->handle($request);
7473
}
7574

76-
public function handleRequestExceptions(string $model, Throwable $e): never
75+
public function handleRequestException(string $model, RequestException $e): never
7776
{
78-
if ($e instanceof PrismException) {
79-
throw $e;
80-
}
81-
82-
if (! $e instanceof RequestException) {
83-
throw PrismException::providerRequestError($model, $e);
84-
}
85-
8677
match ($e->response->getStatusCode()) {
8778
429 => throw PrismRateLimitedException::make(
8879
rateLimits: $this->processRateLimits($e->response),

src/Providers/Gemini/Handlers/Cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected function sendRequest(): array
4949
try {
5050
$response = $this->client->post('/cachedContents', $request);
5151
} catch (Throwable $e) {
52-
$this->handleRequestExceptions($this->model, $e);
52+
$this->handleRequestException($this->model, $e);
5353
}
5454

5555
return $response->json();

src/Providers/Gemini/Handlers/Text.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Prism\Prism\Providers\Gemini\Handlers;
66

7-
use Exception;
87
use Illuminate\Http\Client\PendingRequest;
98
use Illuminate\Http\Client\Response as ClientResponse;
109
use Illuminate\Support\Arr;
@@ -89,7 +88,7 @@ protected function sendRequest(Request $request): ClientResponse
8988
]);
9089

9190
if ($request->tools() !== [] && $request->providerTools() != []) {
92-
throw new Exception('Use of provider tools with custom tools is not currently supported by Gemini.');
91+
throw new PrismException('Use of provider tools with custom tools is not currently supported by Gemini.');
9392
}
9493

9594
$tools = [];

src/Providers/Groq/Groq.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Prism\Prism\Structured\Response as StructuredResponse;
2121
use Prism\Prism\Text\Request as TextRequest;
2222
use Prism\Prism\Text\Response as TextResponse;
23-
use Throwable;
2423

2524
class Groq extends Provider
2625
{
@@ -47,16 +46,8 @@ public function structured(StructuredRequest $request): StructuredResponse
4746
return $handler->handle($request);
4847
}
4948

50-
public function handleRequestExceptions(string $model, Throwable $e): never
49+
public function handleRequestException(string $model, RequestException $e): never
5150
{
52-
if ($e instanceof PrismException) {
53-
throw $e;
54-
}
55-
56-
if (! $e instanceof RequestException) {
57-
throw PrismException::providerRequestError($model, $e);
58-
}
59-
6051
match ($e->response->getStatusCode()) {
6152
429 => throw PrismRateLimitedException::make(
6253
rateLimits: $this->processRateLimits($e->response),

src/Providers/Groq/Maps/ToolChoiceMap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Prism\Prism\Providers\Groq\Maps;
66

7-
use InvalidArgumentException;
87
use Prism\Prism\Enums\ToolChoice;
8+
use Prism\Prism\Exceptions\PrismException;
99

1010
class ToolChoiceMap
1111
{
@@ -26,7 +26,7 @@ public static function map(string|ToolChoice|null $toolChoice): string|array|nul
2626
return match ($toolChoice) {
2727
ToolChoice::Auto => 'auto',
2828
null => $toolChoice,
29-
default => throw new InvalidArgumentException('Invalid tool choice')
29+
default => throw new PrismException('Invalid tool choice')
3030
};
3131
}
3232
}

src/Providers/Mistral/Mistral.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use Prism\Prism\Text\Request as TextRequest;
2929
use Prism\Prism\Text\Response as TextResponse;
3030
use Prism\Prism\ValueObjects\Messages\Support\Document;
31-
use Throwable;
3231

3332
class Mistral extends Provider
3433
{
@@ -107,16 +106,8 @@ public function stream(TextRequest $request): Generator
107106
return $handler->handle($request);
108107
}
109108

110-
public function handleRequestExceptions(string $model, Throwable $e): never
109+
public function handleRequestException(string $model, RequestException $e): never
111110
{
112-
if ($e instanceof PrismException) {
113-
throw $e;
114-
}
115-
116-
if (! $e instanceof RequestException) {
117-
throw PrismException::providerRequestError($model, $e);
118-
}
119-
120111
match ($e->response->getStatusCode()) {
121112
429 => throw PrismRateLimitedException::make(
122113
rateLimits: $this->processRateLimits($e->response),

src/Providers/OpenAI/Handlers/Images.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Illuminate\Http\Client\PendingRequest;
88
use Illuminate\Http\Client\Response as ClientResponse;
9-
use Prism\Prism\Exceptions\PrismException;
109
use Prism\Prism\Images\Request;
1110
use Prism\Prism\Images\Response;
1211
use Prism\Prism\Images\ResponseBuilder;
@@ -16,7 +15,6 @@
1615
use Prism\Prism\ValueObjects\GeneratedImage;
1716
use Prism\Prism\ValueObjects\Meta;
1817
use Prism\Prism\ValueObjects\Usage;
19-
use Throwable;
2018

2119
class Images
2220
{
@@ -53,11 +51,7 @@ public function handle(Request $request): Response
5351

5452
protected function sendRequest(Request $request): ClientResponse
5553
{
56-
try {
57-
return $this->client->post('images/generations', ImageRequestMap::map($request));
58-
} catch (Throwable $e) {
59-
throw PrismException::providerRequestError($request->model(), $e);
60-
}
54+
return $this->client->post('images/generations', ImageRequestMap::map($request));
6155
}
6256

6357
/**

src/Providers/OpenAI/OpenAI.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use Prism\Prism\Structured\Response as StructuredResponse;
2929
use Prism\Prism\Text\Request as TextRequest;
3030
use Prism\Prism\Text\Response as TextResponse;
31-
use Throwable;
3231

3332
class OpenAI extends Provider
3433
{
@@ -96,16 +95,8 @@ public function stream(TextRequest $request): Generator
9695
return $handler->handle($request);
9796
}
9897

99-
public function handleRequestExceptions(string $model, Throwable $e): never
98+
public function handleRequestException(string $model, RequestException $e): never
10099
{
101-
if ($e instanceof PrismException) {
102-
throw $e;
103-
}
104-
105-
if (! $e instanceof RequestException) {
106-
throw PrismException::providerRequestError($model, $e);
107-
}
108-
109100
match ($e->response->getStatusCode()) {
110101
429 => throw PrismRateLimitedException::make(
111102
rateLimits: $this->processRateLimits($e->response),

src/Providers/Provider.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Prism\Prism\Text\Chunk;
1717
use Prism\Prism\Text\Request as TextRequest;
1818
use Prism\Prism\Text\Response as TextResponse;
19-
use Throwable;
2019

2120
abstract class Provider
2221
{
@@ -48,16 +47,8 @@ public function stream(TextRequest $request): Generator
4847
throw PrismException::unsupportedProviderAction(__METHOD__, class_basename($this));
4948
}
5049

51-
public function handleRequestExceptions(string $model, Throwable $e): never
50+
public function handleRequestException(string $model, RequestException $e): never
5251
{
53-
if ($e instanceof PrismException) {
54-
throw $e;
55-
}
56-
57-
if (! $e instanceof RequestException) {
58-
throw PrismException::providerRequestError($model, $e);
59-
}
60-
6152
throw PrismException::providerRequestError($model, $e);
6253
}
6354
}

src/Structured/PendingRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Prism\Prism\Structured;
66

7+
use Illuminate\Http\Client\RequestException;
78
use Prism\Prism\Concerns\ConfiguresClient;
89
use Prism\Prism\Concerns\ConfiguresModels;
910
use Prism\Prism\Concerns\ConfiguresProviders;
@@ -14,7 +15,6 @@
1415
use Prism\Prism\Concerns\HasSchema;
1516
use Prism\Prism\Exceptions\PrismException;
1617
use Prism\Prism\ValueObjects\Messages\UserMessage;
17-
use Throwable;
1818

1919
class PendingRequest
2020
{
@@ -41,8 +41,8 @@ public function asStructured(): Response
4141

4242
try {
4343
return $this->provider->structured($request);
44-
} catch (Throwable $e) {
45-
$this->provider->handleRequestExceptions($request->model(), $e);
44+
} catch (RequestException $e) {
45+
$this->provider->handleRequestException($request->model(), $e);
4646
}
4747
}
4848

src/Testing/PrismFake.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Prism\Prism\Embeddings\Request as EmbeddingRequest;
1212
use Prism\Prism\Embeddings\Response as EmbeddingResponse;
1313
use Prism\Prism\Enums\FinishReason;
14-
use Prism\Prism\Exceptions\PrismException;
1514
use Prism\Prism\Images\Request as ImageRequest;
1615
use Prism\Prism\Images\Response as ImageResponse;
1716
use Prism\Prism\Providers\Provider;
@@ -25,7 +24,6 @@
2524
use Prism\Prism\ValueObjects\GeneratedImage;
2625
use Prism\Prism\ValueObjects\Meta;
2726
use Prism\Prism\ValueObjects\Usage;
28-
use Throwable;
2927

3028
class PrismFake extends Provider
3129
{
@@ -146,11 +144,6 @@ public function stream(TextRequest $request): Generator
146144
yield from $this->chunksFromTextResponse($fixture);
147145
}
148146

149-
public function handleRequestExceptions(string $model, Throwable $e): never
150-
{
151-
throw PrismException::providerRequestError($model, $e);
152-
}
153-
154147
/**
155148
* @param array<string, mixed> $config
156149
*/

src/Text/PendingRequest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Prism\Prism\Text;
66

77
use Generator;
8+
use Illuminate\Http\Client\RequestException;
89
use Prism\Prism\Concerns\ConfiguresClient;
910
use Prism\Prism\Concerns\ConfiguresGeneration;
1011
use Prism\Prism\Concerns\ConfiguresModels;
@@ -17,7 +18,6 @@
1718
use Prism\Prism\Concerns\HasTools;
1819
use Prism\Prism\Exceptions\PrismException;
1920
use Prism\Prism\ValueObjects\Messages\UserMessage;
20-
use Throwable;
2121

2222
class PendingRequest
2323
{
@@ -46,8 +46,8 @@ public function asText(): Response
4646

4747
try {
4848
return $this->provider->text($request);
49-
} catch (Throwable $e) {
50-
$this->provider->handleRequestExceptions($request->model(), $e);
49+
} catch (RequestException $e) {
50+
$this->provider->handleRequestException($request->model(), $e);
5151
}
5252
}
5353

@@ -64,8 +64,8 @@ public function asStream(): Generator
6464
foreach ($chunks as $chunk) {
6565
yield $chunk;
6666
}
67-
} catch (Throwable $e) {
68-
$this->provider->handleRequestExceptions($request->model(), $e);
67+
} catch (RequestException $e) {
68+
$this->provider->handleRequestException($request->model(), $e);
6969
}
7070
}
7171

tests/Providers/Groq/GroqTextTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
->using('groq', 'gpt-4')
143143
->withPrompt('Who are you?')
144144
->withToolChoice(ToolChoice::Any)
145-
->generate();
145+
->asText();
146146
});
147147
});
148148

tests/TestDoubles/TestProvider.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Prism\Prism\ValueObjects\Meta;
2222
use Prism\Prism\ValueObjects\ProviderResponse;
2323
use Prism\Prism\ValueObjects\Usage;
24-
use Throwable;
2524

2625
class TestProvider extends Provider
2726
{
@@ -86,6 +85,10 @@ public function embeddings(EmbeddingRequest $request): EmbeddingResponse
8685
return $this->responses[$this->callCount - 1] ?? new EmbeddingResponse(
8786
embeddings: [],
8887
usage: new EmbeddingsUsage(10),
88+
meta: new Meta(
89+
id: '123',
90+
model: 'your-model',
91+
)
8992
);
9093
}
9194

@@ -114,11 +117,6 @@ public function stream(TextRequest $request): Generator
114117
throw PrismException::unsupportedProviderAction(__METHOD__, class_basename($this));
115118
}
116119

117-
public function handleRequestExceptions(string $model, Throwable $e): never
118-
{
119-
throw PrismException::providerRequestError($model, $e);
120-
}
121-
122120
public function withResponse(StructuredResponse|TextResponse $response): Provider
123121
{
124122
$this->responses[] = $response;

0 commit comments

Comments
 (0)