Skip to content

Commit

Permalink
feat: pass error data for debugging purpose with exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mitulgolakiya committed Aug 4, 2022
1 parent 58743d0 commit fcfaf94
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Client/EnvatoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private function makeApiCall($method, $url, $options)

return $this->parseResponse($response);
} catch (Exception $e) {
LaravelEnvatoUtils::handleEnvatoException($e);
LaravelEnvatoUtils::handleEnvatoException($e, $options);
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/Exceptions/EnvatoException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@ class EnvatoException extends Exception
*/
public $error;

/**
* @var array
*/
public $errorData;

/**
* @param string $error
* @param string $message
* @param int $code
* @param array $errorData
* @param Throwable|null $previous
*/
public function __construct($error, $message = '', $code = 0, Throwable $previous = null)
public function __construct($error, $message = '', $code = 0, Throwable $previous = null, $errorData = [])
{
parent::__construct($message, $code, $previous);
$this->error = $error;
$this->errorData = $errorData;
}
}
8 changes: 4 additions & 4 deletions src/Managers/AuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ public function refreshToken()
'base_uri' => config('laravel-envato.api_endpoint'),
]);

$options = ['form_params' => $params];

try {
$response = $client->request('POST', 'token', [
'form_params' => $params,
]);
$response = $client->request('POST', 'token', $options);
} catch (Exception $e) {
LaravelEnvatoUtils::handleEnvatoException($e);
LaravelEnvatoUtils::handleEnvatoException($e, $options);
}

if ($response->getStatusCode() === 200) {
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/LaravelEnvatoUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LaravelEnvatoUtils
* @throws EnvatoException
* @throws EnvatoRateLimitException
*/
public static function handleEnvatoException($e)
public static function handleEnvatoException($e, $errorData = [])
{
/** @var ClientException $e */
if ($e->getCode() === Response::HTTP_TOO_MANY_REQUESTS) {
Expand All @@ -26,7 +26,7 @@ public static function handleEnvatoException($e)
$response = json_decode($e->getResponse()->getBody()->getContents(), true);

if (isset($response['error']) and isset($response['error_description'])) {
throw new EnvatoException($response['error'], $response['error_description'], $e->getCode(), $e);
throw new EnvatoException($response['error'], $response['error_description'], $e->getCode(), $e, $errorData);
}

throw $e;
Expand Down

0 comments on commit fcfaf94

Please sign in to comment.