Skip to content

Commit

Permalink
Merge pull request #1 from clear01/master
Browse files Browse the repository at this point in the history
Add better exception for PacketAttributesFault
  • Loading branch information
Salamek authored Jul 7, 2020
2 parents 5437acc + 1ebfe89 commit 931cb46
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/ApiRest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,15 @@ private function callApi($method, $object)

/**
* @param array $result
* @throws RestFault
* @throws RestFault|PacketAttributesFault
*/
private function proccessResult($result)
{
if ($result->status == 'fault')
{
if ($result->fault = 'PacketAttributesFault') {
throw new PacketAttributesFault($result->detail->attributes->fault);
}
throw new RestFault($result->fault.': '.$result->string.json_encode($result->detail));
}
}
Expand Down Expand Up @@ -249,4 +252,4 @@ public function senderGetReturnRouting(/*string*/ $senderLabel)
{
return $this->callApi(__FUNCTION__, ['senderLabel' => $senderLabel]);
}
}
}
32 changes: 32 additions & 0 deletions src/Exception/PacketAttributesFault.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Salamek\Zasilkovna\Exception;

use function is_array;

class PacketAttributesFault extends \Exception
{

private $fails;

public function __construct($fails)
{

$fails = !is_array($fails) ? [$fails] : $fails;

foreach ($fails as $fail) {
$this->fails[$fail->name] = $fail->fault;
}
parent::__construct($this->__toString());
}

public function __toString()
{
$string = '';
foreach ($this->fails as $name => $fail) {
$string .= $name . ': ' . $fail . ', ';
}

return trim($string, ', ');
}
}

0 comments on commit 931cb46

Please # to comment.