Skip to content

Commit 9636ebf

Browse files
author
bryanadamloh97
committed
Added exceptions for all drivers
1 parent fe76f7c commit 9636ebf

11 files changed

+131
-13
lines changed

src/Contracts/Shortener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface Shortener
77
/**
88
* Shorten the given URL
99
*
10-
* @param string $url
10+
* @param $url
1111
* @param array $options
1212
* @return string
1313
*/

src/Drivers/BitLyDriverShortener.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
namespace CodeOfDigital\LaravelUrlShortener\Drivers;
44

5+
use CodeOfDigital\LaravelUrlShortener\Exceptions\BadRequestException;
6+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidApiTokenException;
7+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidDataException;
8+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidResponseException;
59
use GuzzleHttp\ClientInterface;
10+
use GuzzleHttp\Exception\RequestException;
611
use GuzzleHttp\Psr7\Request;
712
use Illuminate\Support\Arr;
813
use Psr\Http\Message\ResponseInterface;
@@ -37,8 +42,23 @@ public function shortenAsync($url, array $options = [])
3742
$options = array_merge_recursive(Arr::add($this->object, 'json.long_url', $url), ['json' => $options]);
3843
$request = new Request('POST', '/v4/shorten');
3944

40-
return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) {
41-
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->link);
42-
});
45+
return $this->client->sendAsync($request, $options)->then(
46+
function (ResponseInterface $response) {
47+
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->link);
48+
},
49+
function (RequestException $e) {
50+
$this->getErrorMessage($e->getCode(), $e->getMessage());
51+
}
52+
);
53+
}
54+
55+
protected function getErrorMessage($code, $message = null)
56+
{
57+
switch ($code) {
58+
case 400: throw new BadRequestException($message);
59+
case 403: throw new InvalidApiTokenException($message);
60+
case 422: throw new InvalidDataException($message);
61+
default: throw new InvalidResponseException($message);
62+
}
4363
}
4464
}

src/Drivers/DriverShortener.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
abstract class DriverShortener implements AsyncShortener
88
{
9+
abstract protected function getErrorMessage($code, $message = null);
10+
911
public function shorten($url, array $options = [])
1012
{
1113
return $this->shortenAsync($url, $options)->wait();

src/Drivers/IsGdDriverShortener.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace CodeOfDigital\LaravelUrlShortener\Drivers;
44

5+
use CodeOfDigital\LaravelUrlShortener\Exceptions\BadRequestException;
6+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidDataException;
7+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidResponseException;
58
use GuzzleHttp\ClientInterface;
9+
use GuzzleHttp\Exception\RequestException;
610
use GuzzleHttp\Psr7\Request;
711
use Illuminate\Support\Arr;
812
use Psr\Http\Message\ResponseInterface;
@@ -33,8 +37,22 @@ public function shortenAsync($url, array $options = [])
3337
$options = array_merge_recursive(Arr::add($this->object, 'query.url', $url), ['query' => $options]);
3438
$request = new Request('GET', '/create.php');
3539

36-
return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) {
37-
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->shorturl);
38-
});
40+
return $this->client->sendAsync($request, $options)->then(
41+
function (ResponseInterface $response) {
42+
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->shorturl);
43+
},
44+
function (RequestException $e) {
45+
$this->getErrorMessage($e->getCode(), $e->getMessage());
46+
}
47+
);
48+
}
49+
50+
protected function getErrorMessage($code, $message = null)
51+
{
52+
switch ($code) {
53+
case 400: throw new BadRequestException($message);
54+
case 406: throw new InvalidDataException($message);
55+
default: throw new InvalidResponseException($message);
56+
}
3957
}
4058
}

src/Drivers/ShorteStDriverShortener.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace CodeOfDigital\LaravelUrlShortener\Drivers;
44

5+
use CodeOfDigital\LaravelUrlShortener\Exceptions\BadRequestException;
6+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidApiTokenException;
7+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidResponseException;
58
use GuzzleHttp\ClientInterface;
9+
use GuzzleHttp\Exception\RequestException;
610
use GuzzleHttp\Psr7\Request;
711
use Illuminate\Support\Arr;
812
use Psr\Http\Message\ResponseInterface;
@@ -36,8 +40,25 @@ public function shortenAsync($url, array $options = [])
3640
$options = array_merge_recursive(Arr::add($this->object, 'json.urlToShorten', $url), ['json' => $options]);
3741
$request = new Request('PUT', '/v1/data/url');
3842

39-
return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) {
40-
return json_decode($response->getBody()->getContents())->shortenedUrl;
41-
});
43+
return $this->client->sendAsync($request, $options)->then(
44+
function (ResponseInterface $response) {
45+
if ($response->getStatusCode() == 302)
46+
$this->getErrorMessage($response->getStatusCode(), "Your API Token is invalid. Please try a new API Token.");
47+
48+
return json_decode($response->getBody()->getContents())->shortenedUrl;
49+
},
50+
function (RequestException $e) {
51+
$this->getErrorMessage($e->getCode(), $e->getMessage());
52+
}
53+
);
54+
}
55+
56+
protected function getErrorMessage($code, $message = null)
57+
{
58+
switch ($code) {
59+
case 302: throw new InvalidApiTokenException($message);
60+
case 400: throw new BadRequestException($message);
61+
default: throw new InvalidResponseException($message);
62+
}
4263
}
4364
}

src/Drivers/TinyUrlDriverShortener.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
namespace CodeOfDigital\LaravelUrlShortener\Drivers;
44

5+
use CodeOfDigital\LaravelUrlShortener\Exceptions\BadRequestException;
6+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidApiTokenException;
7+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidDataException;
8+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidResponseException;
9+
use CodeOfDigital\LaravelUrlShortener\Exceptions\MethodNotAllowedException;
510
use GuzzleHttp\ClientInterface;
11+
use GuzzleHttp\Exception\RequestException;
612
use GuzzleHttp\Psr7\Request;
713
use Illuminate\Support\Arr;
814
use Psr\Http\Message\ResponseInterface;
@@ -37,8 +43,24 @@ public function shortenAsync($url, array $options = [])
3743
$options = array_merge_recursive(Arr::add($this->object, 'json.url', $url), ['json' => $options]);
3844
$request = new Request('POST', '/create');
3945

40-
return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) {
41-
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->data->tiny_url);
42-
});
46+
return $this->client->sendAsync($request, $options)->then(
47+
function (ResponseInterface $response) {
48+
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->data->tiny_url);
49+
},
50+
function (RequestException $e) {
51+
$this->getErrorMessage($e->getCode(), $e->getMessage());
52+
}
53+
);
54+
}
55+
56+
protected function getErrorMessage($code, $message = null)
57+
{
58+
switch ($code) {
59+
case 400: throw new BadRequestException($message);
60+
case 401: throw new InvalidApiTokenException($message);
61+
case 405: throw new MethodNotAllowedException($message);
62+
case 422: throw new InvalidDataException($message);
63+
default: throw new InvalidResponseException($message);
64+
}
4365
}
4466
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Exceptions;
4+
5+
class BadRequestException extends \RuntimeException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Exceptions;
4+
5+
class InvalidApiTokenException extends \RuntimeException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Exceptions;
4+
5+
class InvalidDataException extends \RuntimeException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Exceptions;
4+
5+
class InvalidResponseException extends \RuntimeException
6+
{
7+
}

0 commit comments

Comments
 (0)