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
37d286ef
Commit
37d286ef
authored
Feb 24, 2013
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'refs/heads/yii-style-fatal-errors'
parents
2c991833
2d5db951
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
125 additions
and
9 deletions
+125
-9
Application.php
framework/base/Application.php
+69
-2
ErrorException.php
framework/base/ErrorException.php
+47
-0
Exception.php
framework/base/Exception.php
+2
-2
error.php
framework/views/error.php
+4
-3
exception.php
framework/views/exception.php
+3
-2
No files found.
framework/base/Application.php
View file @
37d286ef
...
...
@@ -97,6 +97,12 @@ class Application extends Module
private
$_language
;
/**
* @var string Used to reserve memory for fatal error handler. This memory
* reserve can be removed if it's OK to write to PHP log only in this particular case.
*/
private
$_memoryReserve
;
/**
* Constructor.
* @param string $id the ID of this application. The ID should uniquely identify the application from others.
* @param string $basePath the base path of this application. This should point to
...
...
@@ -110,6 +116,7 @@ class Application extends Module
$this
->
setBasePath
(
$basePath
);
if
(
YII_ENABLE_ERROR_HANDLER
)
{
ini_set
(
'display_errors'
,
0
);
set_exception_handler
(
array
(
$this
,
'handleException'
));
set_error_handler
(
array
(
$this
,
'handleError'
),
error_reporting
());
}
...
...
@@ -142,6 +149,50 @@ class Application extends Module
$this
->
_ended
=
true
;
$this
->
afterRequest
();
}
if
(
YII_ENABLE_ERROR_HANDLER
)
{
$error
=
error_get_last
();
if
(
isset
(
$error
[
'type'
])
&&
in_array
(
$error
[
'type'
],
ErrorException
::
getFatalCodes
()))
{
unset
(
$this
->
_memoryReserve
);
$exception
=
new
ErrorException
(
$error
[
'message'
],
$error
[
'type'
],
$error
[
'type'
],
$error
[
'file'
],
$error
[
'line'
]);
if
(
function_exists
(
'xdebug_get_function_stack'
))
{
$trace
=
array_slice
(
array_reverse
(
xdebug_get_function_stack
()),
4
,
-
1
);
foreach
(
$trace
as
&
$frame
)
{
if
(
!
isset
(
$frame
[
'function'
]))
{
$frame
[
'function'
]
=
'unknown'
;
}
// XDebug < 2.1.1: http://bugs.xdebug.org/view.php?id=695
if
(
!
isset
(
$frame
[
'type'
]))
{
$frame
[
'type'
]
=
'::'
;
}
// XDebug has a different key name
$frame
[
'args'
]
=
array
();
if
(
isset
(
$frame
[
'params'
])
&&
!
isset
(
$frame
[
'args'
]))
{
$frame
[
'args'
]
=
$frame
[
'params'
];
}
}
$ref
=
new
\ReflectionProperty
(
'Exception'
,
'trace'
);
$ref
->
setAccessible
(
true
);
$ref
->
setValue
(
$exception
,
$trace
);
}
$this
->
logException
(
$exception
);
if
((
$handler
=
$this
->
getErrorHandler
())
!==
null
)
{
$handler
->
handle
(
$exception
);
}
else
{
$this
->
renderException
(
$exception
);
}
$status
=
1
;
}
}
if
(
$exit
)
{
exit
(
$status
);
}
...
...
@@ -155,6 +206,10 @@ class Application extends Module
public
function
run
()
{
$this
->
beforeRequest
();
// Allocating twice more than required to display memory exhausted error
// in case of trying to allocate last 1 byte while all memory is taken.
$this
->
_memoryReserve
=
str_repeat
(
'x'
,
1024
*
256
);
register_shutdown_function
(
array
(
$this
,
'end'
),
0
,
false
);
$status
=
$this
->
processRequest
();
$this
->
afterRequest
();
return
$status
;
...
...
@@ -375,12 +430,24 @@ class Application extends Module
* @param string $message the error message
* @param string $file the filename that the error was raised in
* @param integer $line the line number the error was raised at
* @throws \ErrorException the error exception
*
* @throws ErrorException
*/
public
function
handleError
(
$code
,
$message
,
$file
,
$line
)
{
if
(
error_reporting
()
!==
0
)
{
throw
new
\ErrorException
(
$message
,
0
,
$code
,
$file
,
$line
);
$exception
=
new
ErrorException
(
$message
,
$code
,
$code
,
$file
,
$line
);
// in case error appeared in __toString method we can't throw any exception
$trace
=
debug_backtrace
(
false
);
array_shift
(
$trace
);
foreach
(
$trace
as
$frame
)
{
if
(
$frame
[
'function'
]
==
'__toString'
)
{
$this
->
handleException
(
$exception
);
}
}
throw
$exception
;
}
}
...
...
framework/base/ErrorException.php
0 → 100644
View file @
37d286ef
<?php
/**
* ErrorException class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\base
;
/**
* ErrorException represents a PHP error.
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class
ErrorException
extends
\ErrorException
{
public
static
function
getFatalCodes
()
{
return
array
(
E_ERROR
,
E_PARSE
,
E_CORE_ERROR
,
E_CORE_WARNING
,
E_COMPILE_ERROR
,
E_COMPILE_WARNING
);
}
/**
* @return string the user-friendly name of this exception
*/
public
function
getName
()
{
$names
=
array
(
E_ERROR
=>
\Yii
::
t
(
'yii|Fatal Error'
),
E_PARSE
=>
\Yii
::
t
(
'yii|Parse Error'
),
E_CORE_ERROR
=>
\Yii
::
t
(
'yii|Core Error'
),
E_COMPILE_ERROR
=>
\Yii
::
t
(
'yii|Compile Error'
),
E_USER_ERROR
=>
\Yii
::
t
(
'yii|User Error'
),
E_WARNING
=>
\Yii
::
t
(
'yii|Warning'
),
E_CORE_WARNING
=>
\Yii
::
t
(
'yii|Core Warning'
),
E_COMPILE_WARNING
=>
\Yii
::
t
(
'yii|Compile Warning'
),
E_USER_WARNING
=>
\Yii
::
t
(
'yii|User Warning'
),
E_STRICT
=>
\Yii
::
t
(
'yii|Strict'
),
E_NOTICE
=>
\Yii
::
t
(
'yii|Notice'
),
E_RECOVERABLE_ERROR
=>
\Yii
::
t
(
'yii|Recoverable Error'
),
E_DEPRECATED
=>
\Yii
::
t
(
'yii|Deprecated'
),
);
return
isset
(
$names
[
$this
->
getCode
()])
?
$names
[
$this
->
getCode
()]
:
\Yii
::
t
(
'yii|Error'
);
}
}
framework/base/Exception.php
View file @
37d286ef
...
...
@@ -24,5 +24,4 @@ class Exception extends \Exception
{
return
\Yii
::
t
(
'yii|Exception'
);
}
}
}
\ No newline at end of file
framework/views/error.php
View file @
37d286ef
...
...
@@ -4,12 +4,13 @@
* @var \yii\base\ErrorHandler $owner
*/
$owner
=
$this
->
owner
;
$title
=
$owner
->
htmlEncode
(
$exception
instanceof
\yii\base\Exception
||
$exception
instanceof
\yii\base\ErrorException
?
$exception
->
getName
()
:
get_class
(
$exception
));
?>
<!DOCTYPE html>
<html>
<head>
<meta
charset=
"utf-8"
/>
<title>
<?php
echo
get_class
(
$exception
)
?>
</title>
<title>
<?php
echo
$title
?>
</title>
<style>
body
{
...
...
@@ -50,8 +51,8 @@ $owner = $this->owner;
</head>
<body>
<h1>
<?php
echo
$
owner
->
htmlEncode
(
$exception
instanceof
\yii\base\Exception
?
$exception
->
getName
()
:
get_class
(
$exception
));
?>
</h1>
<h2>
<?php
echo
nl2br
(
$owner
->
htmlEncode
(
$exception
->
getMessage
()))
?>
</h2>
<h1>
<?php
echo
$
title
?>
</h1>
<h2>
<?php
echo
nl2br
(
$owner
->
htmlEncode
(
$exception
->
getMessage
()))
?>
</h2>
<p>
The above error occurred while the Web server was processing your request.
</p>
...
...
framework/views/exception.php
View file @
37d286ef
...
...
@@ -4,12 +4,13 @@
* @var \yii\base\ErrorHandler $owner
*/
$owner
=
$this
->
owner
;
$title
=
$owner
->
htmlEncode
(
$exception
instanceof
\yii\base\Exception
||
$exception
instanceof
\yii\base\ErrorException
?
$exception
->
getName
()
.
' ('
.
get_class
(
$exception
)
.
')'
:
get_class
(
$exception
));
?>
<!DOCTYPE html>
<html>
<head>
<meta
charset=
"utf-8"
/>
<title>
<?php
echo
get_class
(
$exception
)
?>
</title>
<title>
<?php
echo
$title
?>
</title>
<style>
html
,
body
,
div
,
span
,
applet
,
object
,
iframe
,
h1
,
h2
,
h3
,
h4
,
h5
,
h6
,
p
,
blockquote
,
pre
,
a
,
abbr
,
acronym
,
address
,
big
,
cite
,
code
,
del
,
dfn
,
em
,
font
,
img
,
ins
,
kbd
,
q
,
s
,
samp
,
small
,
strike
,
strong
,
sub
,
sup
,
tt
,
var
,
b
,
u
,
i
,
center
,
dl
,
dt
,
dd
,
ol
,
ul
,
li
,
fieldset
,
form
,
label
,
legend
,
table
,
caption
,
tbody
,
tfoot
,
thead
,
tr
,
th
,
td
{
border
:
0
;
outline
:
0
;
font-size
:
100%
;
vertical-align
:
baseline
;
background
:
transparent
;
margin
:
0
;
padding
:
0
;}
body
{
line-height
:
1
;}
...
...
@@ -160,7 +161,7 @@ $owner = $this->owner;
<body>
<div
class=
"container"
>
<h1>
<?php
echo
get_class
(
$exception
)
?>
</h1>
<h1>
<?php
echo
$title
?>
</h1>
<p
class=
"message"
>
<?php
echo
nl2br
(
$owner
->
htmlEncode
(
$exception
->
getMessage
()))
?>
...
...
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