diff --git a/src/Http/Request.php b/src/Http/Request.php index d12bffe..e66a7e5 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -88,13 +88,31 @@ protected function handleResponse(Response $response) throw new ItemNotFoundException(isset($content->error) ? $content->error : "Error unknown."); } - throw new PipedriveException( - isset($content->error) ? $content->error->message : "Error unknown." - ); + $this->throwPipedriveException($content); } return $response; } + + /** + * Throws PipedriveException with message depending on content. + * + * @param string $content + */ + protected function throwPipedriveException($content) + { + if (!isset($content->error)) + { + throw new PipedriveException('Error unknown.'); + } + + if (property_exists($content->error, 'message')) + { + throw new PipedriveException($content->error->message); + } + + throw new PipedriveException($content->error); + } /** * Set the endpoint name.