Skip to content

No information is returned when a request fails #1

@jfrosorio

Description

@jfrosorio

Taking one of your examples:

<?php

$vendus = new Vendus\Api('YOUR_API_KEY');
$params = array(
    'fiscal_id'  => '223098091', 
    'name'       => 'Alberto Lopes', 
    'address'    => 'Av. Sousa Magalhães, 126', 
    'postalcode' => '4100-039', 
    'city'       => 'Lisboa', 
    'phone'      => '210 192 930', 
    'mobile'     => '918 876 546', 
    'email'      => 'alberto.lopes@dominio.pt', 
    'website'    => 'https://www.dominio.pt', 
    'country'    => 'PT', 
);

$client = $vendus->clients->create($params);

If for some reason the request fails, the create() method always returns false and we have no idea on what went wrong.
One possible solution would be to refactor the Client's _execute() method and return the response data if its status != 20X. Something like this:

// lines 229 - 231
if ($this->_responseStatus != '200' && $this->_responseStatus != '201') {
    return $this->_response;
}

But even that doesn't solve all the problems, because in a real application, one should be able to do something like:

$vendus = new Vendus\Api('YOUR_API_KEY');

$client = null;

try{
    $client = $vendus->clients->create($params);
}catch(\Exception $e){
    echo $e->getMessage();
}

A more suitable solution would be to throw an exception from the Client class:

if ($this->_responseStatus != '200' && $this->_responseStatus != '201') {
    throw new \Exception($this->_response);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions