-
Notifications
You must be signed in to change notification settings - Fork 47
/
GerencianetException.php
executable file
·72 lines (56 loc) · 2.2 KB
/
GerencianetException.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace Gerencianet\Exception;
use Exception;
class GerencianetException extends Exception
{
private $error;
private $errorDescription;
public function __construct($exception, $code)
{
$error = $exception;
if ($exception instanceof \GuzzleHttp\Psr7\Stream) {
$error = $this->parseStream($exception);
}
$this->apiReturns($error, $code);
}
private function apiReturns($error, $code){
if (isset($error['message'])) {
$message = $error['message'];
$this->code = $code;
$this->errorDescription = $error['message'];
} else if (isset($error['error'])) { // error API Cobranças
$message = isset($error['error_description']['message']) ? $error['error_description']['message'] : $error['error_description'];
$this->code = $error['code'];
$this->error = $error['error'];
$this->errorDescription = $error['error_description'];
} else if (isset($error['type'])) { // error API cobv e reports
$this->code = $error['status'];
$this->error = $error['title'] . ". " . $error['detail'];
$this->errorDescription = $error['violacoes'];
} else { // error API Pix
$message = (isset($error['erros']['mensagem']) ? $error['mensagem'] . ": " . $error['caminho'] . " " . $error['erros']['mensagem'] : $error['mensagem'] . ": " . $error['mensagem']);
$this->code = $code;
$this->error = (isset($error['erros']) ? $error['mensagem'] : $error['nome']);
$this->errorDescription = (isset($error['erros']) ? $error['erros'] : $error['mensagem']);
}
parent::__construct($message, $this->code);
}
private function parseStream($stream)
{
$error = '';
while (!$stream->eof()) {
$error .= $stream->read(1024);
}
return json_decode($error, true);
}
public function __toString()
{
return 'Error ' . $this->code . ': ' . $this->message . "\n";
}
public function __get($property)
{
if (property_exists($this, $property)) {
return $this->$property;
}
}
}