Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
8ee92fdb
Commit
8ee92fdb
authored
Aug 10, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added ErrorAction.
parent
dedad248
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
108 additions
and
0 deletions
+108
-0
ErrorAction.php
framework/yii/web/ErrorAction.php
+108
-0
No files found.
framework/yii/web/ErrorAction.php
0 → 100644
View file @
8ee92fdb
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\web
;
use
Yii
;
use
yii\base\Action
;
use
yii\base\Exception
;
use
yii\base\UserException
;
/**
* ErrorAction displays application errors using a specified view.
*
* To use ErrorAction, you need to do the following steps:
*
* First, declare an action of ErrorAction type in the `actions()` method of your `SiteController`
* class (or whatever controller you prefer), like the following:
*
* ```php
* public function actions()
* {
* return array(
* 'error' => array(
* 'class' => 'yii\web\ErrorAction',
* ),
* );
* }
* ```
*
* Then, create a view file for this action. If the route of your error action is `site/error`, then
* the view file should be `views/site/error.php`. In this view file, the following variables are available:
*
* - `$name`: the error name
* - `$message`: the error message
* - `$exception`: the exception being handled
*
* Finally, configure the "errorHandler" application component as follows,
*
* ```php
* 'errorHandler' => array(
* 'errorAction' => 'site/error',
* )
* ```
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
ErrorAction
extends
Action
{
/**
* @var string the view file to be rendered. If not set, it will take the value of [[id]].
* That means, if you name the action as "error" in "SiteController", then the view name
* would be "error", and the corresponding view file would be "views/site/error.php".
*/
public
$view
;
/**
* @var string the name of the error when the exception name cannot be determined.
* Defaults to "Error".
*/
public
$defaultName
;
/**
* @var string the message to be displayed when the exception message contains sensitive information.
* Defaults to "An internal server error occurred.".
*/
public
$defaultMessage
;
public
function
run
()
{
if
(
!
(
$exception
=
Yii
::
$app
->
getErrorHandler
()
->
exception
))
{
return
''
;
}
if
(
$exception
instanceof
HttpException
)
{
$code
=
$exception
->
statusCode
;
}
else
{
$code
=
$exception
->
getCode
();
}
if
(
$exception
instanceof
Exception
)
{
$name
=
$exception
->
getName
();
}
else
{
$name
=
$this
->
defaultName
?:
Yii
::
t
(
'yii'
,
'Error'
);
}
if
(
$code
)
{
$name
.=
" (#
$code
)"
;
}
if
(
$exception
instanceof
UserException
)
{
$message
=
$exception
->
getMessage
();
}
else
{
$message
=
$this
->
defaultMessage
?:
Yii
::
t
(
'yii'
,
'An internal server error occurred.'
);
}
if
(
Yii
::
$app
->
getRequest
()
->
getIsAjax
())
{
return
"
$name
:
$message
"
;
}
else
{
return
$this
->
controller
->
render
(
$this
->
view
?:
$this
->
id
,
array
(
'name'
=>
$name
,
'message'
=>
$message
,
'exception'
=>
$exception
,
));
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment