1212namespace Symfony \AI \Platform \Bridge \OpenRouter \Embeddings ;
1313
1414use Symfony \AI \Platform \Bridge \OpenRouter \Embeddings ;
15+ use Symfony \AI \Platform \Exception \AuthenticationException ;
16+ use Symfony \AI \Platform \Exception \BadRequestException ;
17+ use Symfony \AI \Platform \Exception \RateLimitExceededException ;
1518use Symfony \AI \Platform \Exception \RuntimeException ;
1619use Symfony \AI \Platform \Model ;
1720use Symfony \AI \Platform \Result \RawResultInterface ;
@@ -31,7 +34,24 @@ public function supports(Model $model): bool
3134
3235 public function convert (RawResultInterface $ result , array $ options = []): VectorResult
3336 {
37+ $ response = $ result ->getObject ();
3438 $ data = $ result ->getData ();
39+
40+ if (401 === $ response ->getStatusCode ()) {
41+ $ errorMessage = json_decode ($ response ->getContent (false ), true )['error ' ]['message ' ];
42+ throw new AuthenticationException ($ errorMessage );
43+ }
44+
45+ if (400 === $ response ->getStatusCode () || 404 === $ response ->getStatusCode ()) {
46+ $ errorMessage = json_decode ($ response ->getContent (false ), true )['error ' ]['message ' ] ?? 'Bad Request ' ;
47+ throw new BadRequestException ($ errorMessage );
48+ }
49+
50+ if (429 === $ response ->getStatusCode ()) {
51+ $ errorMessage = json_decode ($ response ->getContent (false ), true )['error ' ]['message ' ] ?? 'Bad Request ' ;
52+ throw new RateLimitExceededException ($ errorMessage );
53+ }
54+
3555 if (!isset ($ data ['data ' ][0 ]['embedding ' ])) {
3656 throw new RuntimeException ('Response does not contain data. ' );
3757 }
0 commit comments