1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\web; |
9
|
|
|
|
10
|
|
|
use yii\base\UserException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* HttpException represents an exception caused by an improper request of the end-user. |
14
|
|
|
* |
15
|
|
|
* HttpException can be differentiated via its [[statusCode]] property value which |
16
|
|
|
* keeps a standard HTTP status code (e.g. 404, 500). Error handlers may use this status code |
17
|
|
|
* to decide how to format the error page. |
18
|
|
|
* |
19
|
|
|
* Throwing an HttpException like in the following example will result in the 404 page to be displayed. |
20
|
|
|
* |
21
|
|
|
* ```php |
22
|
|
|
* if ($item === null) { // item does not exist |
23
|
|
|
* throw new \yii\web\HttpException(404, 'The requested Item could not be found.'); |
24
|
|
|
* } |
25
|
|
|
* ``` |
26
|
|
|
* |
27
|
|
|
* @author Qiang Xue <[email protected]> |
28
|
|
|
* @since 2.0 |
29
|
|
|
*/ |
30
|
|
|
class HttpException extends UserException |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var int HTTP status code, such as 403, 404, 500, etc. |
34
|
|
|
*/ |
35
|
|
|
public $statusCode; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor. |
40
|
|
|
* @param int $status HTTP status code, such as 404, 500, etc. |
41
|
|
|
* @param string $message error message |
42
|
|
|
* @param int $code error code |
43
|
|
|
* @param \Exception $previous The previous exception used for the exception chaining. |
44
|
|
|
*/ |
45
|
20 |
|
public function __construct($status, $message = null, $code = 0, \Exception $previous = null) |
46
|
|
|
{ |
47
|
20 |
|
$this->statusCode = $status; |
48
|
20 |
|
parent::__construct($message, $code, $previous); |
49
|
20 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string the user-friendly name of this exception |
53
|
|
|
*/ |
54
|
3 |
|
public function getName() |
55
|
|
|
{ |
56
|
3 |
|
if (isset(Response::$httpStatuses[$this->statusCode])) { |
57
|
3 |
|
return Response::$httpStatuses[$this->statusCode]; |
58
|
|
|
} else { |
59
|
|
|
return 'Error'; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|