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