blockcypher / php-client

PHP wrapper for the BlockCypher APIs
Apache License 2.0
51 stars 85 forks source link

Call to method WebHook::getAllErrorMessages() causes an exception #4

Closed rtarnaud closed 8 years ago

rtarnaud commented 8 years ago

Hi,

I think there's another issue with the getAllErrorMessages method of WebHook. I invoke it after a standard web hook creation:


        $webHook = new \BlockCypher\Api\WebHook();
        $webHook->setUrl($url);
        $webHook->setEvent('tx-confirmation');
        $webHook->setHash($hash);

        $apiContext = new \BlockCypher\Rest\ApiContext(new \BlockCypher\Auth\SimpleTokenCredential($this->blockcypherToken));

        $webHookClient = new \BlockCypher\Client\WebHookClient($apiContext);
        $webHookModified = $webHookClient->create($webHook);

        $webHookModified->getAllErrorMessages();

Fair enough, I spotted this bug recently with an invalid hash ($hash) but I think the call to the method also failed with a valid hash.

FYI, here's the call stack on crash:

{
    "error": {
        "code": 500,
        "message": "Internal Server Error",
        "exception": [{
                "message": "Invalid argument supplied for foreach()",
                "class": "ErrorException",
                "trace": [{
                        "namespace": "",
                        "short_class": "",
                        "class": "",
                        "type": "",
                        "function": "",
                        "file": "C:\\wamp\\www\\XXX\\vendor\\blockcypher\\php-client\\lib\\BlockCypher\\Common\\BlockCypherBaseModel.php",
                        "line": 87,
                        "args": []
                    }, {
                        "namespace": "",
                        "short_class": "AppKernel",
                        "class": "AppKernel",
                        "type": "->",
                        "function": "{closure}",
                        "file": "C:\\wamp\\www\\XXX\\vendor\\blockcypher\\php-client\\lib\\BlockCypher\\Common\\BlockCypherBaseModel.php",
                        "line": 87,
                        "args": [["string", "2"], ["string", "Invalid argument supplied for foreach()"], ["string", "C:\\wamp\\www\\XXX\\vendor\\blockcypher\\php-client\\lib\\BlockCypher\\Common\\BlockCypherBaseModel.php"], ["string", "87"], ["array", {
                                    "errorMessages": ["array", []]
                                }]]
                    }, {
                        "namespace": "BlockCypher\\Common",
                        "short_class": "BlockCypherBaseModel",
                        "class": "BlockCypher\\Common\\BlockCypherBaseModel",
                        "type": "->",
                        "function": "getErrorMessages",
                        "file": "C:\\wamp\\www\\XXX\\vendor\\blockcypher\\php-client\\lib\\BlockCypher\\Common\\BlockCypherBaseModel.php",
                        "line": 75,
                        "args": []
                    }, {
                        "namespace": "BlockCypher\\Common",
                        "short_class": "BlockCypherBaseModel",
                        "class": "BlockCypher\\Common\\BlockCypherBaseModel",
                        "type": "->",
                        "function": "getAllErrorMessages",
                        "file": "C:\\wamp\\www\\XXX\\src\\XXX\\CurrencyBundle\\Controller\\TransactionRestController.php",
                        "line": 36,
                        "args": []
                    }, {
                        "namespace": "XXX\\CurrencyBundle\\Controller",
                        "short_class": "TransactionRestController",
                        "class": "XXX\\CurrencyBundle\\Controller\\TransactionRestController",
                        "type": "->",
                        "function": "postTransactionFundingAction",
                        "file": null,
                        "line": null,
                        "args": [["object", "Symfony\\Component\\HttpFoundation\\Request"]]
                    }, {
                        "namespace": "",
                        "short_class": "",
                        "class": "",
                        "type": "",
                        "function": "call_user_func_array",
                        "file": "C:\\wamp\\www\\XXX\\app\\bootstrap.php.cache",
                        "line": 3109,
                        "args": [["array", [["object", "XXX\\CurrencyBundle\\Controller\\TransactionRestController"], ["string", "postTransactionFundingAction"]]], ["array", [["object", "Symfony\\Component\\HttpFoundation\\Request"]]]]
                    }, {
                        "namespace": "Symfony\\Component\\HttpKernel",
                        "short_class": "HttpKernel",
                        "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
                        "type": "->",
                        "function": "handleRaw",
                        "file": "C:\\wamp\\www\\XXX\\app\\bootstrap.php.cache",
                        "line": 3071,
                        "args": [["object", "Symfony\\Component\\HttpFoundation\\Request"], ["string", "1"]]
                    }, {
                        "namespace": "Symfony\\Component\\HttpKernel",
                        "short_class": "HttpKernel",
                        "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
                        "type": "->",
                        "function": "handle",
                        "file": "C:\\wamp\\www\\XXX\\app\\bootstrap.php.cache",
                        "line": 3222,
                        "args": [["object", "Symfony\\Component\\HttpFoundation\\Request"], ["string", "1"], ["boolean", true]]
                    }, {
                        "namespace": "Symfony\\Component\\HttpKernel\\DependencyInjection",
                        "short_class": "ContainerAwareHttpKernel",
                        "class": "Symfony\\Component\\HttpKernel\\DependencyInjection\\ContainerAwareHttpKernel",
                        "type": "->",
                        "function": "handle",
                        "file": "C:\\wamp\\www\\XXX\\app\\bootstrap.php.cache",
                        "line": 2444,
                        "args": [["object", "Symfony\\Component\\HttpFoundation\\Request"], ["string", "1"], ["boolean", true]]
                    }, {
                        "namespace": "Symfony\\Component\\HttpKernel",
                        "short_class": "Kernel",
                        "class": "Symfony\\Component\\HttpKernel\\Kernel",
                        "type": "->",
                        "function": "handle",
                        "file": "C:\\wamp\\www\\XXX\\web\\app_dev.php",
                        "line": 28,
                        "args": [["object", "Symfony\\Component\\HttpFoundation\\Request"]]
                    }]
            }]
    }
}

Any ideas on this one?

Thanks!

josecelano commented 8 years ago

Hi @rtarnaud . Sorry I have been very busy. It's a bug in this method:

`` php public function getErrorMessages() { $errorMessages = array(); if (is_array($this->errors)) { // <!-- FIXED foreach ($this->errors as $error) { $errorMessages[] = $error->getError(); } } return $errorMessages; }



If the json response does not contain any error this methods fails. I am gooing to fix it, add a test, and build a new release. In the mean time you can check if `getErrors()`  returns an array before calling `getErrorMessages()` mthod.

Thanks you very much.
rtarnaud commented 8 years ago

Ok, thanks!

josecelano commented 8 years ago

Fixed but I still have to generate a new release with other minor bug fixes.