Skip to content

Commit

Permalink
Correcting response from server in case when we have 404 error (#891)
Browse files Browse the repository at this point in the history
* Correcting response from server in case when we have 404 error

* small fixes
  • Loading branch information
oleksandr-mykhailenko authored Jan 8, 2024
1 parent 454ab2b commit 89628fc
Show file tree
Hide file tree
Showing 73 changed files with 369 additions and 299 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Change Log

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## 4.0.1
- Fix wrong classes in tests
- Fixed response in case of 404 http error. Respect server error message

## 4.0
- SubAccount support @oleksandr-mykhailenko in #886
- Requests of behalf of Sub Account

## 3.6.2
- Bugfix: TypeError caused by improper use of new self() instead of new static() in base class method

Expand Down
67 changes: 58 additions & 9 deletions src/Exception/HttpClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Mailgun\Exception;
use Psr\Http\Message\ResponseInterface;
use Throwable;

/**
* @author Tobias Nyholm <[email protected]>
Expand Down Expand Up @@ -48,7 +49,11 @@ public function __construct(string $message, int $code, ResponseInterface $respo
}
}

public static function badRequest(ResponseInterface $response)
/**
* @param ResponseInterface $response
* @return HttpClientException
*/
public static function badRequest(ResponseInterface $response): HttpClientException
{
$body = $response->getBody()->__toString();
if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
Expand All @@ -63,37 +68,72 @@ public static function badRequest(ResponseInterface $response)
return new self($message, 400, $response);
}

public static function unauthorized(ResponseInterface $response)
/**
* @param ResponseInterface $response
* @return HttpClientException
*/
public static function unauthorized(ResponseInterface $response): HttpClientException
{
return new self('Your credentials are incorrect.', 401, $response);
}

public static function requestFailed(ResponseInterface $response)
/**
* @param ResponseInterface $response
* @return HttpClientException
*/
public static function requestFailed(ResponseInterface $response): HttpClientException
{
return new self('Parameters were valid but request failed. Try again.', 402, $response);
}

public static function notFound(ResponseInterface $response)
/**
* @param ResponseInterface $response
* @return HttpClientException
*/
public static function notFound(ResponseInterface $response): HttpClientException
{
return new self('The endpoint you have tried to access does not exist. Check if the domain matches the domain you have configure on Mailgun.', 404, $response);
$serverMessage = [];
$defaultMessage = 'The endpoint you have tried to access does not exist. Check if the domain matches the domain you have configure on Mailgun.';
try {
$serverMessage = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
} catch (Throwable $throwable) {
}

return new self($serverMessage['message'] ?? $defaultMessage, 404, $response);
}

public static function conflict(ResponseInterface $response)
/**
* @param ResponseInterface $response
* @return HttpClientException
*/
public static function conflict(ResponseInterface $response): HttpClientException
{
return new self('Request conflicts with current state of the target resource.', 409, $response);
}

public static function payloadTooLarge(ResponseInterface $response)
/**
* @param ResponseInterface $response
* @return HttpClientException
*/
public static function payloadTooLarge(ResponseInterface $response): HttpClientException
{
return new self('Payload too large, your total attachment size is too big.', 413, $response);
}

public static function tooManyRequests(ResponseInterface $response)
/**
* @param ResponseInterface $response
* @return HttpClientException
*/
public static function tooManyRequests(ResponseInterface $response): HttpClientException
{
return new self('Too many requests.', 429, $response);
}

public static function forbidden(ResponseInterface $response)
/**
* @param ResponseInterface $response
* @return HttpClientException
*/
public static function forbidden(ResponseInterface $response): HttpClientException
{
$body = $response->getBody()->__toString();
if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
Expand All @@ -108,16 +148,25 @@ public static function forbidden(ResponseInterface $response)
return new self($message, 403, $response);
}

/**
* @return ResponseInterface|null
*/
public function getResponse(): ?ResponseInterface
{
return $this->response;
}

/**
* @return array
*/
public function getResponseBody(): array
{
return $this->responseBody;
}

/**
* @return int
*/
public function getResponseCode(): int
{
return $this->responseCode;
Expand Down
18 changes: 15 additions & 3 deletions src/Exception/HttpServerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,29 @@
*/
final class HttpServerException extends \RuntimeException implements Exception
{
public static function serverError(int $httpStatus = 500)
/**
* @param int $httpStatus
* @return HttpServerException
*/
public static function serverError(int $httpStatus = 500): HttpServerException
{
return new self('An unexpected error occurred at Mailgun\'s servers. Try again later and contact support if the error still exists.', $httpStatus);
}

public static function networkError(\Throwable $previous)
/**
* @param \Throwable $previous
* @return HttpServerException
*/
public static function networkError(\Throwable $previous): HttpServerException
{
return new self('Mailgun\'s servers are currently unreachable.', 0, $previous);
}

public static function unknownHttpResponseCode(int $code)
/**
* @param int $code
* @return HttpServerException
*/
public static function unknownHttpResponseCode(int $code): HttpServerException
{
return new self(sprintf('Unknown HTTP response code ("%d") received from the API server', $code));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@

use PHPUnit\Framework\TestCase;

abstract class BaseModelTest extends TestCase
abstract class BaseModel extends TestCase
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace Mailgun\Tests\Model\Domain;

use Mailgun\Model\Domain\OpenTracking;
use Mailgun\Tests\Model\BaseModelTest;
use Mailgun\Model\Domain\ClickTracking as ClickTrackingAlias;
use Mailgun\Tests\Model\BaseModel;

class OpenTrackingTest extends BaseModelTest
class ClickTracking extends BaseModel
{
public function testCreate()
{
Expand All @@ -24,7 +24,7 @@ public function testCreate()
"active": true
}
JSON;
$model = OpenTracking::create(json_decode($json, true));
$model = ClickTrackingAlias::create(json_decode($json, true));
$this->assertTrue($model->isActive());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace Mailgun\Tests\Model\Domain;

use Mailgun\Model\Domain\ConnectionResponse;
use Mailgun\Tests\Model\BaseModelTest;
use Mailgun\Model\Domain\ConnectionResponse as ConnectionResponseAlias;
use Mailgun\Tests\Model\BaseModel;

class ConnectionResponseTest extends BaseModelTest
class ConnectionResponse extends BaseModel
{
public function testCreate()
{
Expand All @@ -27,7 +27,7 @@ public function testCreate()
}
}
JSON;
$model = ConnectionResponse::create(json_decode($json, true));
$model = ConnectionResponseAlias::create(json_decode($json, true));
$this->assertFalse($model->getRequireTLS());
$this->assertFalse($model->getSkipVerification());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace Mailgun\Tests\Model\Domain;

use Mailgun\Model\Domain\CreateCredentialResponse;
use Mailgun\Tests\Model\BaseModelTest;
use Mailgun\Model\Domain\CreateCredentialResponse as CreateCredentialResponseAlias;
use Mailgun\Tests\Model\BaseModel;

class CreateCredentialResponseTest extends BaseModelTest
class CreateCredentialResponse extends BaseModel
{
public function testCreate()
{
Expand All @@ -24,7 +24,7 @@ public function testCreate()
"message": "Created 1 credentials pair(s)"
}
JSON;
$model = CreateCredentialResponse::create(json_decode($json, true));
$model = CreateCredentialResponseAlias::create(json_decode($json, true));
$this->assertNotEmpty($model->getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace Mailgun\Tests\Model\Domain;

use Mailgun\Model\Domain\CreateResponse;
use Mailgun\Tests\Model\BaseModelTest;
use Mailgun\Model\Domain\CreateResponse as CreateResponseAlias;
use Mailgun\Tests\Model\BaseModel;

class CreateResponseTest extends BaseModelTest
class CreateResponse extends BaseModel
{
public function testCreate()
{
Expand Down Expand Up @@ -68,7 +68,7 @@ public function testCreate()
}

JSON;
$model = CreateResponse::create(json_decode($json, true));
$model = CreateResponseAlias::create(json_decode($json, true));
$this->assertNotEmpty($model->getMessage());
$this->assertNotEmpty($model->getDomain());
$this->assertNotEmpty($model->getInboundDNSRecords());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace Mailgun\Tests\Model\Domain;

use Mailgun\Model\Domain\CredentialResponse;
use Mailgun\Tests\Model\BaseModelTest;
use Mailgun\Model\Domain\CredentialResponse as CredentialResponseAlias;
use Mailgun\Tests\Model\BaseModel;

class CredentialResponseTest extends BaseModelTest
class CredentialResponse extends BaseModel
{
public function testCreate()
{
Expand All @@ -38,7 +38,7 @@ public function testCreate()
]
}
JSON;
$model = CredentialResponse::create(json_decode($json, true));
$model = CredentialResponseAlias::create(json_decode($json, true));
$this->assertEquals(2, $model->getTotalCount());
$this->assertCount(2, $model->getCredentials());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace Mailgun\Tests\Model\Domain;

use Mailgun\Model\Domain\CredentialResponseItem;
use Mailgun\Tests\Model\BaseModelTest;
use Mailgun\Model\Domain\CredentialResponseItem as CredentialResponseItemAlias;
use Mailgun\Tests\Model\BaseModel;

class CredentialResponseItemTest extends BaseModelTest
class CredentialResponseItem extends BaseModel
{
public function testCreate()
{
Expand All @@ -27,7 +27,7 @@ public function testCreate()
"login": "user"
}
JSON;
$model = CredentialResponseItem::create(json_decode($json, true));
$model = CredentialResponseItemAlias::create(json_decode($json, true));
$this->assertEquals('user', $model->getLogin());
$this->assertEquals('[email protected]', $model->getMailbox());
$this->assertEquals('5', $model->getSizeBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace Mailgun\Tests\Model\Domain;

use Mailgun\Model\Domain\DeleteCredentialResponse;
use Mailgun\Tests\Model\BaseModelTest;
use Mailgun\Model\Domain\DeleteCredentialResponse as DeleteCredentialResponseAlias;
use Mailgun\Tests\Model\BaseModel;

class DeleteCredentialResponseTest extends BaseModelTest
class DeleteCredentialResponse extends BaseModel
{
public function testCreate()
{
Expand All @@ -26,7 +26,7 @@ public function testCreate()
}

JSON;
$model = DeleteCredentialResponse::create(json_decode($json, true));
$model = DeleteCredentialResponseAlias::create(json_decode($json, true));
$this->assertNotEmpty($model->getMessage());
$this->assertEmpty($model->getError());
$this->assertNotEmpty($model->getSpec());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace Mailgun\Tests\Model\Domain;

use Mailgun\Model\Domain\DeleteResponse;
use Mailgun\Tests\Model\BaseModelTest;
use Mailgun\Model\Domain\DeleteResponse as DeleteResponseAlias;
use Mailgun\Tests\Model\BaseModel;

class DeleteResponseTest extends BaseModelTest
class DeleteResponse extends BaseModel
{
public function testCreate()
{
Expand All @@ -24,7 +24,7 @@ public function testCreate()
"message": "Domain has been deleted"
}
JSON;
$model = DeleteResponse::create(json_decode($json, true));
$model = DeleteResponseAlias::create(json_decode($json, true));
$this->assertNotEmpty($model->getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace Mailgun\Tests\Model\Domain;

use Mailgun\Model\Domain\DnsRecord;
use Mailgun\Tests\Model\BaseModelTest;
use Mailgun\Model\Domain\DnsRecord as DnsRecordAlias;
use Mailgun\Tests\Model\BaseModel;

class DnsRecordTest extends BaseModelTest
class DnsRecord extends BaseModel
{
public function testCreate()
{
Expand All @@ -27,7 +27,7 @@ public function testCreate()
"value": "v=spf1 include:mailgun.org ~all"
}
JSON;
$model = DnsRecord::create(json_decode($json, true));
$model = DnsRecordAlias::create(json_decode($json, true));
$this->assertNotEmpty($model->getType());
$this->assertNotEmpty($model->getValidity());
$this->assertTrue($model->isValid());
Expand Down
Loading

0 comments on commit 89628fc

Please sign in to comment.