crisp-im / php-crisp-api

:elephant: Crisp API PHP Wrapper
https://docs.crisp.chat/guides/rest-api/
MIT License
56 stars 28 forks source link

Error Handling #22

Closed AlexeyKosov closed 3 years ago

AlexeyKosov commented 4 years ago

The current implementation does not allow finding out if a request returned an error and what the error was. If, for example, the server returns {"error":true,"reason":"people_exists","data":{}}, the API client will return only {} (only the data key is returned).

  public function createNewPeopleProfile($websiteId, $params) {
    $result = $this->crisp->_rest->post(
      "website/$websiteId/people/profile",
      json_encode($params)
    );
    return $result->decode_response()["data"];
  }

It would make sense to throw an error in such a case or add any other ability to handle errors.

kester36 commented 4 years ago

I have the same problem. API client returns only 'data' key for updatePeopleProfile(). But its empty as for good ('updated') case as for bad (_'people_notfound') case. Does not exists success/failed marker.

-- Otherwise, for updatePeopleData() API client returns all response data.

eliottvincent commented 3 years ago

Hello guys! I've issued an update on the PHP wrapper to have more complete error details.

For example, this code...

try {
  $result = $CrispClient->buckets->generate([
    "foo" => "bar"
  ]);

  var_dump($result);
} catch (\Crisp\CrispException $exception) {
  var_dump($exception->getError());
}

...throws an exception with all the necessary details:

array(4) {
  ["reason"]=>
  string(5) "error"
  ["message"]=>
  string(12) "invalid_data"
  ["code"]=>
  int(400)
  ["data"]=>
  array(2) {
    ["namespace"]=>
    string(8) "response"
    ["message"]=>
    string(66) "Got response error: data should have required property 'namespace'"
  }
}

This is available in version 1.6.3 😊