Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiErrorException
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getApiError
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStatusCode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 report
n/a
0 / 0
n/a
0 / 0
1
1<?php
2
3namespace App\Exceptions\Api\V2;
4
5use App\Enums\ApiError;
6use Exception;
7use Illuminate\Support\Facades\Log;
8use Throwable;
9
10/**
11 * This would be thrown if any of the case
12 * referenced in \App\Enums\ApiError would
13 * occur within the crm
14 *
15 * On the client an ApiErrorException would
16 * also be throw with the ApiError corresponding to
17 * the situation
18 *
19 * @package LDCCrmClient
20 * @subpackage exceptions
21 */
22class ApiErrorException extends Exception
23{
24    /**
25     * Api Error
26     * This value makes the error more explicit.
27     * Handling these errors are normal as inputs or
28     * request may be undesired or refused by the crm.
29     *
30     * @var \App\Enums\ApiError
31     */
32    protected $error;
33
34    /**
35     * This is an explicit error message,
36     * ou may print this to the front or not depending
37     * on $error value
38     *
39     * @var string
40     */
41    protected $message;
42
43    /**
44     * This is the http code returned by the crm.
45     * This value could be used to handle errors.
46     * But please take in consideration $error first
47     *
48     * @var integer
49     */
50    protected $httpCode;
51
52    /**
53     * Create a new exception base on ApiError enum.
54     *
55     * @param ApiError       $error
56     * @param integer        $httpCode
57     * @param string         $message
58     * @param Throwable|null $previous
59     */
60    public function __construct(ApiError $error, int $httpCode = 400, string $message = '', Throwable $previous = null)
61    {
62        $this->error    = $error;
63        $this->message  = $message;
64        $this->httpCode = $httpCode;
65        parent::__construct(
66            strlen($this->message) ? $this->message : $error->label,
67            $error->value,
68            $previous
69        );
70    }
71
72    /**
73     * Get the Api Error
74     *
75     * @return ApiError
76     */
77    public function getApiError(): ApiError
78    {
79        return $this->error;
80    }
81
82    /**
83     * Get the http code
84     *
85     * @return integer
86     */
87    public function getStatusCode(): int
88    {
89        return $this->httpCode;
90    }
91
92    /**
93     * This is used by laravel only.
94     * It uses Log class to print the error.
95     *
96     * @return void
97     */
98    public function report()
99    {
100        // @codeCoverageIgnoreStart
101        Log::debug(sprintf('CRM API: %s', $this->getMessage()));
102        // @codeCoverageIgnoreEnd
103    }
104}