@@ -87,9 +87,9 @@ class ApiClient
8787 * @param string|null $url URL base de la API.
8888 */
8989 public function __construct (
90- string $ token = null ,
91- string $ rut = null ,
92- string $ url = null
90+ ? string $ token = null ,
91+ ? string $ rut = null ,
92+ ? string $ url = null
9393 ) {
9494 $ this ->apiToken = $ token ?: $ this ->env ('CONTAFI_API_TOKEN ' );
9595 if (!$ this ->apiToken ) {
@@ -146,7 +146,7 @@ public function setRut(string $rut): static
146146 *
147147 * @return string|null
148148 */
149- public function getLastUrl (): string
149+ public function getLastUrl (): ? string
150150 {
151151 return $ this ->lastUrl ;
152152 }
@@ -156,7 +156,7 @@ public function getLastUrl(): string
156156 *
157157 * @return \Psr\Http\Message\ResponseInterface|null
158158 */
159- public function getLastResponse (): ResponseInterface
159+ public function getLastResponse (): ? ResponseInterface
160160 {
161161 return $ this ->lastResponse ;
162162 }
@@ -354,60 +354,26 @@ public function consume(
354354 // realizar consulta HTTP
355355 try {
356356 $ this ->lastResponse = $ client ->request (
357- $ method ,
358- $ this ->lastUrl ,
359- $ options
357+ method: $ method ,
358+ uri: $ this ->lastUrl ,
359+ options: $ options
360360 );
361- } catch (\GuzzleHttp \Exception \GuzzleException $ e ) {
361+ } catch (\GuzzleHttp \Exception \BadResponseException $ e ) {
362+ // Obtener la respuesta de la llamada.
362363 $ this ->lastResponse = $ e ->getResponse ();
364+
365+ // Si no es un error 401 con problema de sesión se lanza la excepción.
363366 $ this ->throwException ();
367+ } catch (\GuzzleHttp \Exception \GuzzleException $ e ) {
368+ throw new ApiException ('Error de conexión con el SII: ' . $ e ->getMessage (), 500 );
364369 }
370+
371+ // Si no se reintentó se lanza excepción por no ser código 200.
365372 if ($ this ->getLastResponse ()->getStatusCode () != 200 ) {
366373 $ this ->throwException ();
367374 }
368- return $ this ;
369- }
370-
371- /**
372- * Extrae información detallada sobre un error a partir de la última respuesta HTTP.
373- *
374- * Este método analiza la última respuesta HTTP para extraer información
375- * detallada sobre un error que ocurrió durante la solicitud. Devuelve un
376- * objeto con los detalles del error, incluyendo el código y el mensaje.
377- *
378- * @return object Detalles del error con propiedades 'code' y 'message'.
379- */
380- private function getError (): object
381- {
382- $ data = $ this ->getBodyDecoded ();
383- $ response = $ this ->getLastResponse ();
384- $ statusCode = $ response ? $ response ->getStatusCode () : null ;
385- $ reasonPhrase = $ response ? $ response
386- ->getReasonPhrase () : 'Sin respuesta ' ;
387375
388- if ($ data ) {
389- $ code = isset ($ data ['code ' ]) ? $ data ['code ' ] : $ statusCode ;
390- $ message = isset (
391- $ data ['message ' ]
392- ) ? $ data ['message ' ] : $ reasonPhrase ;
393- } else {
394- $ code = $ statusCode ;
395- $ message = $ reasonPhrase ;
396- }
397-
398- // Se maneja el caso donde no se encuentra un mensaje de error específico
399- if (!$ message || $ message === '' ) {
400- $ message = sprintf (
401- '[ContaFi API] Código HTTP %d: %s ' ,
402- $ code ,
403- $ reasonPhrase
404- );
405- }
406-
407- return (object )[
408- 'code ' => $ code ,
409- 'message ' => $ message ,
410- ];
376+ return $ this ;
411377 }
412378
413379 /**
@@ -422,8 +388,25 @@ private function getError(): object
422388 */
423389 private function throwException (): ApiException
424390 {
425- $ error = $ this ->getError ();
426- throw new ApiException ($ error ->message , $ error ->code );
391+ $ response = $ this ->getLastResponse ();
392+
393+ if (!$ response ) {
394+ throw new ApiException (
395+ message: 'Error desconocido. ' ,
396+ code: 500 ,
397+ responseBody: null
398+ );
399+ }
400+
401+ $ status = $ response ->getStatusCode ();
402+ $ body = (string ) $ response ->getBody ();
403+ $ message = $ body !== '' ? $ body : $ response ->getReasonPhrase ();
404+
405+ throw new ApiException (
406+ message: $ message ,
407+ code: $ status ,
408+ responseBody: $ body
409+ );
427410 }
428411
429412 /**
0 commit comments