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
bb290691
Commit
bb290691
authored
Mar 29, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
adjusted guide and small code adjustements
parent
33543cbd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
28 deletions
+23
-28
error.md
docs/guide/error.md
+15
-15
ErrorHandler.php
framework/base/ErrorHandler.php
+8
-13
No files found.
docs/guide/error.md
View file @
bb290691
...
...
@@ -10,7 +10,7 @@ use Yii;
try
{
10
/
0
;
}
catch
(
ErrorException
)
{
}
catch
(
ErrorException
$e
)
{
Yii
::
warning
(
"Tried dividing by zero."
);
}
...
...
@@ -19,38 +19,39 @@ try {
As demonstrated above you may handle errors using
`try`
-
`catch`
.
Second, even fatal errors in Yii are rendered in a nice way. This means that in debugging mode, you can trace the causes
of fatal errors in order to more quickly identify the cause of the problem.
Rendering errors in a dedicated controller action
-------------------------------------------------
The default Yii error page is great when developing a site, and is acceptable for production sites if
`YII_DEBUG`
is turned off in your bootstrap
index.php
file. But you may want to customize the default error page to make it
is turned off in your bootstrap
`index.php`
file. But you may want to customize the default error page to make it
more suitable for your project.
The easiest way to create a custom error page it is to use a dedicated controller action for error rendering. First,
you'll need to configure the
`errorHandler`
component in the application's configuration:
```
php
return
[
// ...
'components'
=>
[
// ...
'components'
=>
[
// ...
'errorHandler'
=>
[
'errorAction'
=>
'site/error'
,
'errorHandler'
=>
[
'errorAction'
=>
'site/error'
,
],
]
```
With that configuration in place, whenever an error occurs, Yii will execute the
"error" action of the "Site"
controller.
With that configuration in place, whenever an error occurs, Yii will execute the
`error`
-action of the
`site`
-
controller.
That action should look for an exception and, if present, render the proper view file, passing along the exception:
```
php
public
function
actionError
()
{
if
(
\Yii
::
$app
->
exception
!==
null
)
{
return
$this
->
render
(
'error'
,
[
'exception'
=>
\Yii
::
$app
->
exception
]);
$exception
=
\Yii
::
$app
->
errorHandler
->
exception
;
if
(
$exception
!==
null
)
{
return
$this
->
render
(
'error'
,
[
'exception'
=>
$exception
]);
}
}
```
...
...
@@ -58,14 +59,13 @@ public function actionError()
Next, you would create the
`views/site/error.php`
file, which would make use of the exception. The exception object has
the following properties:
-
`statusCode`
: the HTTP status code (e.g. 403, 500). Available for
HTTP exceptions
only.
-
`statusCode`
: the HTTP status code (e.g. 403, 500). Available for
[
[yii\web\HttpException|HTTP exceptions
]
]
only.
-
`code`
: the code of the exception.
-
`type`
: the error type (e.g. HttpException, PHP Error).
-
`message`
: the error message.
-
`file`
: the name of the PHP script file where the error occurs.
-
`line`
: the line number of the code where the error occurs.
-
`trace`
: the call stack of the error.
-
`source`
: the context source code where the error occurs.
Rendering errors without a dedicated controller action
------------------------------------------------------
...
...
@@ -91,4 +91,4 @@ automatically be used. The view will be passed three variables:
-
`$message`
: the error message
-
`$exception`
: the exception being handled
The
`$exception`
object will have the same properties outlined above.
The
`$exception`
object will have the same properties
as
outlined above.
framework/base/ErrorHandler.php
View file @
bb290691
...
...
@@ -81,11 +81,7 @@ class ErrorHandler extends Component
if
(
$this
->
discardExistingOutput
)
{
$this
->
clearOutput
();
}
if
(
PHP_SAPI
===
'cli'
)
{
Console
::
stderr
(
$this
->
renderException
(
$exception
));
}
else
{
echo
$this
->
renderException
(
$exception
);
}
$this
->
renderException
(
$exception
);
if
(
!
YII_ENV_TEST
)
{
exit
(
1
);
}
...
...
@@ -168,11 +164,7 @@ class ErrorHandler extends Component
if
(
$this
->
discardExistingOutput
)
{
$this
->
clearOutput
();
}
if
(
PHP_SAPI
===
'cli'
)
{
Console
::
stderr
(
$this
->
renderException
(
$exception
));
}
else
{
echo
$this
->
renderException
(
$exception
);
}
$this
->
renderException
(
$exception
);
exit
(
1
);
}
}
...
...
@@ -180,7 +172,6 @@ class ErrorHandler extends Component
/**
* Renders an exception without using rich format.
* @param \Exception $exception the exception to be rendered.
* @return string the rendering result
*/
protected
function
renderException
(
$exception
)
{
...
...
@@ -202,7 +193,12 @@ class ErrorHandler extends Component
}
else
{
$message
=
$this
->
formatMessage
(
'Error: '
)
.
$exception
->
getMessage
();
}
return
$message
.
"
\n
"
;
if
(
PHP_SAPI
===
'cli'
)
{
Console
::
stderr
(
$message
.
"
\n
"
);
}
else
{
echo
$message
.
"
\n
"
;
}
}
/**
...
...
@@ -229,7 +225,6 @@ class ErrorHandler extends Component
*/
protected
function
logException
(
$exception
)
{
// TODO logger may not be registered
$category
=
get_class
(
$exception
);
if
(
$exception
instanceof
HttpException
)
{
$category
=
'yii\\web\\HttpException:'
.
$exception
->
statusCode
;
...
...
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