ldc-flucollector/app/Exceptions/Api/ApiErrorException.php

100 lines
2.3 KiB
PHP
Raw Normal View History

2023-09-20 12:12:56 +02:00
<?php
namespace App\Exceptions\Api;
2023-10-02 09:35:06 +02:00
use App\Enums\Api\ApiError;
2023-09-20 12:12:56 +02:00
use Illuminate\Support\Facades\Log;
/**
* This would be thrown if any of the case
* referenced in \App\Enums\ApiError would
2023-10-02 09:35:06 +02:00
* occur within flucollector
2023-09-20 12:12:56 +02:00
*
* On the client an ApiErrorException would
* also be throw with the ApiError corresponding to
* the situation
*/
2023-10-02 09:35:06 +02:00
class ApiErrorException extends \RuntimeException
2023-09-20 12:12:56 +02:00
{
/**
* Api Error
* This value makes the error more explicit.
* Handling these errors are normal as inputs or
2023-10-02 09:35:06 +02:00
* request may be undesired or refused by flucollector.
2023-09-20 12:12:56 +02:00
*
2023-10-02 09:35:06 +02:00
* @var \App\Enums\Api\ApiError
2023-09-20 12:12:56 +02:00
*/
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;
/**
2023-10-02 09:35:06 +02:00
* This is the http code returned by flucollector.
2023-09-20 12:12:56 +02:00
* 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.
*
2023-10-02 09:35:06 +02:00
* @param \App\Enums\Api\ApiError $error
* @param integer $httpCode
* @param string $message
* @param \Throwable|null $previous
2023-09-20 12:12:56 +02:00
*/
2023-10-02 09:35:06 +02:00
public function __construct(ApiError $error, int $httpCode = 400, string $message = '', \Throwable $previous = null)
2023-09-20 12:12:56 +02:00
{
$this->error = $error;
$this->message = $message;
$this->httpCode = $httpCode;
parent::__construct(
2023-10-02 14:58:25 +02:00
strlen($this->message) ? $this->message : $error->label(),
$error->value(),
2023-09-20 12:12:56 +02:00
$previous
);
}
/**
* Get the Api Error
*
2023-10-02 09:35:06 +02:00
* @return \App\Enums\Api\ApiError
2023-09-20 12:12:56 +02:00
*/
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
2023-10-02 09:35:06 +02:00
Log::debug(sprintf('FluxCollector API: %s', $this->getMessage()));
2023-09-20 12:12:56 +02:00
// @codeCoverageIgnoreEnd
}
}