2021-03-14 10:45:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
2021-07-28 14:18:47 +02:00
|
|
|
namespace App\Exceptions\Api\V2;
|
2021-03-14 10:45:42 +01:00
|
|
|
|
|
|
|
|
use App\Enums\ApiError;
|
|
|
|
|
use Exception;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
use Throwable;
|
|
|
|
|
|
2021-08-04 15:07:25 +02:00
|
|
|
/**
|
|
|
|
|
* This would be thrown if any of the case
|
|
|
|
|
* referenced in \App\Enums\ApiError would
|
2023-09-06 17:24:53 +02:00
|
|
|
* occur within the crm.
|
2021-08-04 15:07:25 +02:00
|
|
|
*
|
|
|
|
|
* On the client an ApiErrorException would
|
|
|
|
|
* also be throw with the ApiError corresponding to
|
2023-09-06 17:24:53 +02:00
|
|
|
* the situation.
|
2021-08-09 08:55:39 +02:00
|
|
|
*
|
|
|
|
|
* @package LDCCrmClient
|
|
|
|
|
* @subpackage exceptions
|
2021-08-04 15:07:25 +02:00
|
|
|
*/
|
2021-03-14 10:45:42 +01:00
|
|
|
class ApiErrorException extends Exception
|
|
|
|
|
{
|
2021-08-04 15:07:25 +02:00
|
|
|
/**
|
|
|
|
|
* Api Error
|
2021-08-09 08:55:39 +02:00
|
|
|
* This value makes the error more explicit.
|
|
|
|
|
* Handling these errors are normal as inputs or
|
|
|
|
|
* request may be undesired or refused by the crm.
|
2021-08-04 15:07:25 +02:00
|
|
|
*
|
|
|
|
|
* @var \App\Enums\ApiError
|
|
|
|
|
*/
|
|
|
|
|
protected $error;
|
2021-03-14 10:45:42 +01:00
|
|
|
|
2021-08-09 08:55:39 +02:00
|
|
|
/**
|
|
|
|
|
* This is an explicit error message,
|
|
|
|
|
* ou may print this to the front or not depending
|
2023-09-06 17:24:53 +02:00
|
|
|
* on $error value.
|
2021-08-09 08:55:39 +02:00
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2021-08-04 17:15:06 +02:00
|
|
|
protected $message;
|
|
|
|
|
|
2021-08-09 08:55:39 +02:00
|
|
|
/**
|
|
|
|
|
* This is the http code returned by the crm.
|
|
|
|
|
* This value could be used to handle errors.
|
|
|
|
|
* But please take in consideration $error first
|
|
|
|
|
*
|
2021-12-19 15:57:24 +01:00
|
|
|
* @var integer
|
2021-08-09 08:55:39 +02:00
|
|
|
*/
|
2021-08-04 15:07:25 +02:00
|
|
|
protected $httpCode;
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Create a new exception base on ApiError enum.
|
|
|
|
|
*
|
2023-09-06 17:24:53 +02:00
|
|
|
* @param \App\Enums\ApiError $error
|
|
|
|
|
* @param integer $httpCode
|
|
|
|
|
* @param string $message
|
|
|
|
|
* @param Throwable|null $previous
|
2021-12-19 15:57:24 +01:00
|
|
|
*/
|
2021-08-04 17:15:06 +02:00
|
|
|
public function __construct(ApiError $error, int $httpCode = 400, string $message = '', Throwable $previous = null)
|
2021-03-14 10:45:42 +01:00
|
|
|
{
|
2021-08-04 17:15:06 +02:00
|
|
|
$this->error = $error;
|
|
|
|
|
$this->message = $message;
|
2021-08-04 15:07:25 +02:00
|
|
|
$this->httpCode = $httpCode;
|
2021-08-04 17:15:06 +02:00
|
|
|
parent::__construct(
|
2023-09-04 13:08:34 +02:00
|
|
|
strlen($this->message) ? $this->message : $error->label(),
|
2023-09-06 17:24:53 +02:00
|
|
|
$error->value(),
|
2021-08-04 17:15:06 +02:00
|
|
|
$previous
|
|
|
|
|
);
|
2021-03-14 10:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-04 15:07:25 +02:00
|
|
|
/**
|
2023-09-06 17:24:53 +02:00
|
|
|
* Get the Api Error.
|
2021-08-04 15:07:25 +02:00
|
|
|
*
|
2023-09-06 17:24:53 +02:00
|
|
|
* @return \App\Enums\ApiError
|
2021-08-04 15:07:25 +02:00
|
|
|
*/
|
|
|
|
|
public function getApiError(): ApiError
|
|
|
|
|
{
|
|
|
|
|
return $this->error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-09-06 17:24:53 +02:00
|
|
|
* Get the http code.
|
2021-08-04 15:07:25 +02:00
|
|
|
*
|
2021-12-19 15:57:24 +01:00
|
|
|
* @return integer
|
2021-08-04 15:07:25 +02:00
|
|
|
*/
|
|
|
|
|
public function getStatusCode(): int
|
2021-03-14 10:45:42 +01:00
|
|
|
{
|
2021-08-04 15:07:25 +02:00
|
|
|
return $this->httpCode;
|
2021-03-14 10:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* This is used by laravel only.
|
|
|
|
|
* It uses Log class to print the error.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-09-06 17:24:53 +02:00
|
|
|
public function report(): void
|
2021-03-14 10:45:42 +01:00
|
|
|
{
|
2021-12-19 15:57:24 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-03-14 10:45:42 +01:00
|
|
|
Log::debug(sprintf('CRM API: %s', $this->getMessage()));
|
2021-12-19 15:57:24 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2021-03-14 10:45:42 +01:00
|
|
|
}
|
|
|
|
|
}
|