Skip to content

Commit

Permalink
Merge pull request #11 from tarfin-labs/add-an-exception-for-unhandle…
Browse files Browse the repository at this point in the history
…d-error-codes

Add an exception for unhandled error codes
  • Loading branch information
frkcn authored Mar 4, 2020
2 parents e3138e0 + d353957 commit 7860bfe
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to `netgsm` will be documented in this file

## Unreleased

## 2.1.0 - 2020-03-04

- added an exception for unhandled netgsm error codes.
- Laravel 7 support added.

## 2.0.0 - 2020-03-02
Expand Down
2 changes: 1 addition & 1 deletion src/Balance/NetgsmAvailableCredit.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function parseResponse(): ?string
$code = $result[0];

if (! in_array($code, $this->successCodes)) {
$message = $this->errorCodes[$code];
$message = $this->errorCodes[$code] ?? NetgsmErrors::SYSTEM_ERROR;
throw new NetgsmException($message, $code);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Balance/NetgsmPackages.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function parseResponse(): array
$availablePackages = [];

if (array_key_exists($this->response, $this->errorCodes)) {
$message = $this->errorCodes[$this->response];
$message = $this->errorCodes[$this->response] ?? NetgsmErrors::SYSTEM_ERROR;
throw new NetgsmException($message, $this->response);
}

Expand Down
1 change: 1 addition & 0 deletions src/NetgsmErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ class NetgsmErrors
const SYSTEM_ERROR = 'Sistem hatası.';
const NETGSM_GENERAL_ERROR = 'Netgsm responded with an error :';
const NO_RECORD = 'No records';
const JOB_ID_NOT_FOUND = 'Job id not found';
}
15 changes: 12 additions & 3 deletions src/Sms/AbstractNetgsmMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Psr\Http\Message\ResponseInterface;
use TarfinLabs\Netgsm\Exceptions\CouldNotSendNotification;
use TarfinLabs\Netgsm\Exceptions\IncorrectPhoneNumberFormatException;
use TarfinLabs\Netgsm\Exceptions\NetgsmException;
use TarfinLabs\Netgsm\NetgsmApiClient;
use TarfinLabs\Netgsm\NetgsmErrors;

Expand Down Expand Up @@ -316,6 +317,7 @@ public function getJobId(): string
*
* @return $this
* @throws CouldNotSendNotification
* @throws NetgsmException
*/
public function parseResponse(): self
{
Expand All @@ -325,12 +327,17 @@ public function parseResponse(): self
throw new CouldNotSendNotification(NetgsmErrors::NETGSM_GENERAL_ERROR);
}

if (! in_array($result[0], self::SUCCESS_CODES)) {
$message = $this->errorCodes[$result[0]];
$code = $result[0];
if (! in_array($code, self::SUCCESS_CODES)) {
$message = $this->errorCodes[$code] ?? NetgsmErrors::SYSTEM_ERROR;
throw new CouldNotSendNotification($message);
}

$this->code = $result[0];
if (! isset($result[1])) {
throw new NetgsmException(NetgsmErrors::JOB_ID_NOT_FOUND);
}

$this->code = $code;
$this->jobId = $result[1];

return $this;
Expand All @@ -342,6 +349,7 @@ public function parseResponse(): self
* @return $this
* @throws CouldNotSendNotification
* @throws GuzzleException
* @throws NetgsmException
*/
protected function sendViaGet(): self
{
Expand All @@ -356,6 +364,7 @@ protected function sendViaGet(): self
* @return $this
* @throws CouldNotSendNotification
* @throws GuzzleException
* @throws NetgsmException
*/
protected function sendViaXml(): self
{
Expand Down
20 changes: 19 additions & 1 deletion tests/NetGsmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Mockery;
use TarfinLabs\Netgsm\Exceptions\CouldNotSendNotification;
use TarfinLabs\Netgsm\Exceptions\IncorrectPhoneNumberFormatException;
use TarfinLabs\Netgsm\Exceptions\NetgsmException;
use TarfinLabs\Netgsm\Netgsm;
use TarfinLabs\Netgsm\Sms\NetgsmSmsMessage;

Expand Down Expand Up @@ -83,7 +84,24 @@ public function should_throw_exception_when_return_code_is_not_success()
->andReturn(new Response(
$status = 200,
$headers = [],
$this->faker->randomElement(['20', '30', '40', '70'])
$this->faker->randomElement(['20', '30', '40', '70', '52'])
));

$this->netgsm->sendSms($this->newSmsMessage());
}

/**
* @test
*/
public function should_throw_exception_when_is_no_job_id_in_response()
{
$this->expectException(NetgsmException::class);

$this->httpClient->shouldReceive('request')
->andReturn(new Response(
$status = 200,
$headers = [],
$this->faker->randomElement(['00', '01', '02'])
));

$this->netgsm->sendSms($this->newSmsMessage());
Expand Down

0 comments on commit 7860bfe

Please sign in to comment.