ldc-crm/app/Exceptions/Api/V2/ApiErrorException.php
Jérémy Munsch 46a47e822d Some php doc
2021-08-09 08:55:39 +02:00

89 lines
1.9 KiB
PHP

<?php
namespace App\Exceptions\Api\V2;
use App\Enums\ApiError;
use Exception;
use Illuminate\Support\Facades\Log;
use Throwable;
/**
* This would be thrown if any of the case
* referenced in \App\Enums\ApiError would
* occur within the crm
*
* On the client an ApiErrorException would
* also be throw with the ApiError corresponding to
* the situation
*
* @package LDCCrmClient
* @subpackage exceptions
*/
class ApiErrorException extends Exception
{
/**
* Api Error
* This value makes the error more explicit.
* Handling these errors are normal as inputs or
* request may be undesired or refused by the crm.
*
* @var \App\Enums\ApiError
*/
protected $error;
/**
* This is an explicit error message,
* ou may print this to the front or not depending
* on $error value
*
* @var string
*/
protected $message;
/**
* This is the http code returned by the crm.
* This value could be used to handle errors.
* But please take in consideration $error first
*
* @var int
*/
protected $httpCode;
public function __construct(ApiError $error, int $httpCode = 400, string $message = '', Throwable $previous = null)
{
$this->error = $error;
$this->message = $message;
$this->httpCode = $httpCode;
parent::__construct(
strlen($this->message) ? $this->message : $error->label,
$error->value,
$previous
);
}
/**
* Get the Api Error
*
* @return ApiError
*/
public function getApiError(): ApiError
{
return $this->error;
}
/**
* Get the http code
*
* @return int
*/
public function getStatusCode(): int
{
return $this->httpCode;
}
public function report()
{
Log::debug(sprintf('CRM API: %s', $this->getMessage()));
}
}