Skip to content

Commit

Permalink
fix: throws actual exceptions instead of their wrappers (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
asadali214 authored Apr 1, 2024
1 parent ec9cc44 commit bb916c4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
7 changes: 1 addition & 6 deletions src/Response/Types/DeserializableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Closure;
use Core\Response\Context;
use Throwable;

class DeserializableType
{
Expand All @@ -29,10 +28,6 @@ public function getFrom(Context $context)
if (is_null($this->deserializerMethod)) {
return null;
}
try {
return Closure::fromCallable($this->deserializerMethod)($context->getResponse()->getBody());
} catch (Throwable $t) {
throw $context->toApiException($t->getMessage());
}
return Closure::fromCallable($this->deserializerMethod)($context->getResponse()->getBody());
}
}
23 changes: 9 additions & 14 deletions src/Response/Types/ResponseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Closure;
use Core\Response\Context;
use Exception;

class ResponseType
{
Expand Down Expand Up @@ -57,20 +56,16 @@ public function getFrom(Context $context)
if (is_null($this->responseClass)) {
return null;
}
try {
if (isset($this->xmlDeserializer)) {
return Closure::fromCallable($this->xmlDeserializer)(
$context->getResponse()->getRawBody(),
$this->responseClass
);
}
return $context->getJsonHelper()->mapClass(
$context->getResponse()->getBody(),
$this->responseClass,
$this->dimensions
if (isset($this->xmlDeserializer)) {
return Closure::fromCallable($this->xmlDeserializer)(
$context->getResponse()->getRawBody(),
$this->responseClass
);
} catch (Exception $e) {
throw $context->toApiException($e->getMessage());
}
return $context->getJsonHelper()->mapClass(
$context->getResponse()->getBody(),
$this->responseClass,
$this->dimensions
);
}
}
10 changes: 6 additions & 4 deletions tests/ApiCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Core\Tests;

use apimatic\jsonmapper\AnyOfValidationException;
use apimatic\jsonmapper\JsonMapperException;
use apimatic\jsonmapper\OneOfValidationException;
use Core\Request\Parameters\AdditionalFormParams;
use Core\Request\Parameters\AdditionalQueryParams;
Expand All @@ -29,6 +30,7 @@
use CoreInterfaces\Http\RetryOption;
use CURLFile;
use Exception;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

class ApiCallTest extends TestCase
Expand Down Expand Up @@ -766,7 +768,7 @@ public function testSendMultipleXMLBodyParams()

public function testReceiveByWrongType()
{
$this->expectException(MockException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('JsonMapper::mapClass() requires second argument to be a class name, ' .
'InvalidClass given.');
MockHelper::newApiCall()
Expand All @@ -786,7 +788,7 @@ public function fakeSerializeBy($argument)

public function testReceiveByWrongDeserializerMethod()
{
$this->expectException(MockException::class);
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid argument found');
MockHelper::newApiCall()
->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')))
Expand Down Expand Up @@ -1326,7 +1328,7 @@ public function testTypeXml()

public function testTypeXmlFailure()
{
$this->expectException(MockException::class);
$this->expectException(Exception::class);
$this->expectExceptionMessage(
'Required value not found at XML path "/mockClass/new1[1]" during deserialization.'
);
Expand All @@ -1349,7 +1351,7 @@ public function testTypeXmlFailure()

public function testTypeInvalidJsonFailure()
{
$this->expectException(MockException::class);
$this->expectException(JsonMapperException::class);
$this->expectExceptionMessage(
'Could not find required constructor arguments for Core\Tests\Mocking\Other\MockClass: body'
);
Expand Down

0 comments on commit bb916c4

Please sign in to comment.