101 lines
2.3 KiB
PHP
101 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Exceptions\Api;
|
|
|
|
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
|
|
*/
|
|
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 integer
|
|
*/
|
|
protected $httpCode;
|
|
|
|
/**
|
|
* Create a new exception base on ApiError enum.
|
|
*
|
|
* @param ApiError $error
|
|
* @param integer $httpCode
|
|
* @param string $message
|
|
* @param Throwable|null $previous
|
|
*/
|
|
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 integer
|
|
*/
|
|
public function getStatusCode(): int
|
|
{
|
|
return $this->httpCode;
|
|
}
|
|
|
|
/**
|
|
* This is used by laravel only.
|
|
* It uses Log class to print the error.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function report()
|
|
{
|
|
// @codeCoverageIgnoreStart
|
|
Log::debug(sprintf('CRM API: %s', $this->getMessage()));
|
|
// @codeCoverageIgnoreEnd
|
|
}
|
|
}
|