infusionsoft / infusionsoft-php

PHP client library for the Infusionsoft API.
https://developer.infusionsoft.com/
Other
130 stars 127 forks source link

Empty HTTP bodies break the REST SDK #162

Open igorsantos07 opened 6 years ago

igorsantos07 commented 6 years ago

This is a two-part problem, actually.

The first one is with the API itself, which seems broken; for some orders I'm requesting it returns a body like "" (i.e. no content); some other orders return a fine JSON body, and that happens both through the SDK and the REST docs. But I guess this is not the place to report API issues, so this is just an introduction.

The actual SDK issue is: find() calls do expect an array to pass into fill(), but as that thing happens, it fails with Uncaught TypeError: Argument 1 passed to Infusionsoft\Api\Rest\RestModel::fill() must be of the type array, null given.

While the API should be fixed, the SDK should fail gracefully when an empty body is given, not only when an empty object/list is returned. API errors should not break the SDKs (and thus, our applications).

mfairch commented 6 years ago

@igorsantos07 Does this happen only on Orders or are there other objects as well?

mfairch commented 6 years ago

@igorsantos07 The api is going to be fixed and an update to the SDK will be out soon that prevents an exception from being thrown.

igorsantos07 commented 6 years ago

I'm not sure it happens on other endpoints, sorry.

brumiser1550 commented 4 years ago

I was about to open a new issue but I think what I have is the root cause of this.

The save() method on line 165 of vendor/infusionsoft/php-sdk/src/Infusionsoft/Api/Rest/RestModel.php cannot handle null responses from Infusionsoft resulting in an uncatchable fatal error.

I figured this out from trying to add a tag through the REST API where the tag category was not valid.

I feel like Infusionsoft should be returning some kind of message saying that the resource doesn't exist, not just a null response.

An easy solution would be to just say:

if(!$data){
    $data = [];
}
$this->fill($data);

because the fill method requires an array.

The Stack trace

PHP Fatal error:  Uncaught TypeError: Argument 1 passed to Infusionsoft\Api\Rest\RestModel::fill() must be of the type array, null given, called in /vendor/infusionsoft/php-sdk/src/Infusionsoft/Api/Rest/RestModel.php on line 177 and defined in /vendor/infusionsoft/php-sdk/src/Infusionsoft/Api/Rest/RestModel.php:391
Stack trace:
#0 /vendor/infusionsoft/php-sdk/src/Infusionsoft/Api/Rest/RestModel.php(177): Infusionsoft\Api\Rest\RestModel->fill(NULL)
#1 /vendor/infusionsoft/php-sdk/src/Infusionsoft/Api/Rest/RestModel.php(155): Infusionsoft\Api\Rest\RestModel->save()
#2 /process.php(918): Infusionsoft\Api\Rest\RestModel->create(Array)
#3 {main}
  thrown in /vendor/infusionsoft/php-sdk/src/Infusionsoft/Api/Rest/RestModel.php on line 391