forked from apoloz/alfa-bank-sdk
refactor(error): handling
This commit is contained in:
@@ -43,7 +43,8 @@ trait HasAlfaErrorHandling
|
||||
if (str_contains($contentType, 'application/json')) {
|
||||
$data = $response->json();
|
||||
$errorCode = $data['error'] ?? $data['ErrorCode'] ?? 'HTTP_' . $response->status();
|
||||
$errorDescription = $data['error_description'] ?? $data['message'] ?? 'Request failed';
|
||||
$rawDescription = $data['error_description'] ?? $data['message'] ?? 'Request failed';
|
||||
$errorDescription = $this->decodeBankMessage($rawDescription);
|
||||
} else {
|
||||
$errorCode = 'HTTP_' . $response->status();
|
||||
$errorDescription = 'Request failed with status ' . $response->status();
|
||||
@@ -55,4 +56,15 @@ trait HasAlfaErrorHandling
|
||||
"({$requestId}) {$errorDescription}"
|
||||
);
|
||||
}
|
||||
|
||||
private function decodeBankMessage(string $message): string
|
||||
{
|
||||
$decoded = preg_replace_callback(
|
||||
'/%u([0-9A-Fa-f]{4})/',
|
||||
fn ($m) => mb_convert_encoding(pack('H*', $m[1]), 'UTF-8', 'UCS-2BE'),
|
||||
$message
|
||||
);
|
||||
|
||||
return urldecode($decoded);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user