@@ -94,9 +94,6 @@ public static function make(
9494
9595 /**
9696 * @return iterable<array{string|null, array<string, mixed>}>
97- *
98- * @throws ExceptionInterface When a handler throws an exception during message processing
99- * @throws \JsonException When JSON encoding of the response fails
10097 */
10198 public function process (string $ input , ?Uuid $ sessionId ): iterable
10299 {
@@ -161,9 +158,11 @@ public function process(string $input, ?Uuid $sessionId): iterable
161158 }
162159
163160 foreach ($ messages as $ message ) {
161+ $ messageId = method_exists ($ message , 'getId ' ) ? $ message ->getId () : 0 ;
162+
164163 if ($ message instanceof InvalidInputMessageException) {
165164 $ this ->logger ->warning ('Failed to create message. ' , ['exception ' => $ message ]);
166- $ error = Error::forInvalidRequest ($ message ->getMessage (), 0 );
165+ $ error = Error::forInvalidRequest ($ message ->getMessage (), $ messageId );
167166 yield [$ this ->encodeResponse ($ error ), []];
168167 continue ;
169168 }
@@ -183,17 +182,17 @@ public function process(string $input, ?Uuid $sessionId): iterable
183182 ['exception ' => $ e ],
184183 );
185184
186- $ error = Error::forMethodNotFound ($ e ->getMessage ());
185+ $ error = Error::forMethodNotFound ($ e ->getMessage (), $ messageId );
187186 yield [$ this ->encodeResponse ($ error ), []];
188187 } catch (\InvalidArgumentException $ e ) {
189188 $ this ->logger ->warning (\sprintf ('Invalid argument: %s ' , $ e ->getMessage ()), ['exception ' => $ e ]);
190189
191- $ error = Error::forInvalidParams ($ e ->getMessage ());
190+ $ error = Error::forInvalidParams ($ e ->getMessage (), $ messageId );
192191 yield [$ this ->encodeResponse ($ error ), []];
193192 } catch (\Throwable $ e ) {
194193 $ this ->logger ->critical (\sprintf ('Uncaught exception: %s ' , $ e ->getMessage ()), ['exception ' => $ e ]);
195194
196- $ error = Error::forInternalError ($ e ->getMessage ());
195+ $ error = Error::forInternalError ($ e ->getMessage (), $ messageId );
197196 yield [$ this ->encodeResponse ($ error ), []];
198197 }
199198 }
@@ -202,7 +201,7 @@ public function process(string $input, ?Uuid $sessionId): iterable
202201 }
203202
204203 /**
205- * @throws \JsonException When JSON encoding fails
204+ * Encodes a response to JSON, handling encoding errors gracefully.
206205 */
207206 private function encodeResponse (Response |Error |null $ response ): ?string
208207 {
@@ -214,11 +213,26 @@ private function encodeResponse(Response|Error|null $response): ?string
214213
215214 $ this ->logger ->info ('Encoding response. ' , ['response ' => $ response ]);
216215
217- if ($ response instanceof Response && [] === $ response ->result ) {
218- return json_encode ($ response , \JSON_THROW_ON_ERROR | \JSON_FORCE_OBJECT );
219- }
216+ try {
217+ if ($ response instanceof Response && [] === $ response ->result ) {
218+ return json_encode ($ response , \JSON_THROW_ON_ERROR | \JSON_FORCE_OBJECT );
219+ }
220220
221- return json_encode ($ response , \JSON_THROW_ON_ERROR );
221+ return json_encode ($ response , \JSON_THROW_ON_ERROR );
222+ } catch (\JsonException $ e ) {
223+ $ this ->logger ->error ('Failed to encode response to JSON. ' , [
224+ 'message_id ' => $ response ->getId (),
225+ 'exception ' => $ e ,
226+ ]);
227+
228+ $ fallbackError = new Error (
229+ id: $ response ->getId (),
230+ code: Error::INTERNAL_ERROR ,
231+ message: 'Response could not be encoded to JSON '
232+ );
233+
234+ return json_encode ($ fallbackError , \JSON_THROW_ON_ERROR );
235+ }
222236 }
223237
224238 /**
0 commit comments