-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathException.php
More file actions
47 lines (41 loc) · 1.35 KB
/
Exception.php
File metadata and controls
47 lines (41 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace Tinify;
class Exception extends \Exception {
public $status;
/**
* @param string $message
* @param string $type
* @param int $status
* @return Exception|AccountException|ClientException|ServerException
*/
public static function create($message, $type, $status) {
if ($status == 401 || $status == 429) {
$klass = "Tinify\AccountException";
} else if($status >= 400 && $status <= 499) {
$klass = "Tinify\ClientException";
} else if($status >= 500 && $status <= 599) {
$klass = "Tinify\ServerException";
} else {
$klass = "Tinify\Exception";
}
if (empty($message)) $message = "No message was provided";
return new $klass($message, $type, $status);
}
/**
* @param string $message
* @param string|null $type
* @param int|null $status
*/
function __construct($message, $type = NULL, $status = NULL) {
$this->status = $status;
if ($status) {
parent::__construct($message . " (HTTP " . $status . "/" . $type . ")");
} else {
parent::__construct($message);
}
}
}
class AccountException extends Exception {}
class ClientException extends Exception {}
class ServerException extends Exception {}
class ConnectionException extends Exception {}