Client.php
9 years ago
Exception.php
10 years ago
Result.php
10 years ago
ResultMeta.php
10 years ago
Source.php
10 years ago
Exception.php
37 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Tinify; |
| 4 | |
| 5 | class Exception extends \Exception { |
| 6 | public $status; |
| 7 | |
| 8 | public static function create($message, $type, $status) { |
| 9 | if ($status == 401 || $status == 403 || $status == 429) { |
| 10 | $klass = "Tinify\AccountException"; |
| 11 | } else if($status >= 400 && $status <= 499) { |
| 12 | $klass = "Tinify\ClientException"; |
| 13 | } else if($status >= 500 && $status <= 599) { |
| 14 | $klass = "Tinify\ServerException"; |
| 15 | } else { |
| 16 | $klass = "Tinify\Exception"; |
| 17 | } |
| 18 | |
| 19 | if (empty($message)) $message = "No message was provided"; |
| 20 | return new $klass($message, $type, $status); |
| 21 | } |
| 22 | |
| 23 | function __construct($message, $type = NULL, $status = NULL) { |
| 24 | $this->status = $status; |
| 25 | if ($status) { |
| 26 | parent::__construct($message . " (HTTP " . $status . "/" . $type . ")"); |
| 27 | } else { |
| 28 | parent::__construct($message); |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | class AccountException extends Exception {} |
| 34 | class ClientException extends Exception {} |
| 35 | class ServerException extends Exception {} |
| 36 | class ConnectionException extends Exception {} |
| 37 |