Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
UserAdminV2
Overview
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
庄欣
UserAdminV2
Commits
5c3c2e2d
Commit
5c3c2e2d
authored
Aug 16, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored error handling for better architecture.
parent
e2c69d0c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
205 additions
and
70 deletions
+205
-70
system/exception/handler.php
+118
-0
system/exception/wrapper.php
+55
-63
system/file.php
+23
-0
system/laravel.php
+9
-7
No files found.
system/exception/handler.php
0 → 100644
View file @
5c3c2e2d
<?php
namespace
System\Exception
;
use
System\View
;
use
System\Config
;
use
System\Response
;
class
Handler
{
/**
* The exception wrapper for the exception being handled.
*
* @var Wrapper
*/
public
$exception
;
/**
* Create a new exception handler instance.
*
* @param Exception $e
* @return void
*/
public
function
__construct
(
$e
)
{
$this
->
exception
=
new
Wrapper
(
$e
);
}
/**
* Create a new exception handler instance.
*
* @param Exception $e
* @return Handler
*/
public
static
function
make
(
$e
)
{
return
new
static
(
$e
);
}
/**
* Handle the exception and display the error report.
*
* The exception will be logged if error logging is enabled.
*
* The output buffer will be cleaned so nothing is sent to the browser except the
* error message. This prevents any views that have already been rendered from
* being shown in an incomplete or erroneous state.
*
* After the exception is displayed, the request will be halted.
*
* @return void
*/
public
function
handle
()
{
if
(
ob_get_level
()
>
0
)
ob_clean
();
if
(
Config
::
get
(
'error.log'
))
$this
->
log
();
$this
->
get_response
(
Config
::
get
(
'error.detail'
))
->
send
();
exit
(
1
);
}
/**
* Log the exception using the logger closure specified in the error configuration.
*
* @return void
*/
private
function
log
()
{
$parameters
=
array
(
$this
->
exception
->
severity
(),
$this
->
exception
->
message
(),
$this
->
exception
->
getTraceAsString
(),
);
call_user_func_array
(
Config
::
get
(
'error.logger'
),
$parameters
);
}
/**
* Get the error report response for the exception.
*
* @param bool $detailed
* @return Resposne
*/
private
function
get_response
(
$detailed
)
{
return
(
$detailed
)
?
$this
->
detailed_response
()
:
$this
->
generic_response
();
}
/**
* Get the detailed error report for the exception.
*
* @return Response
*/
private
function
detailed_response
()
{
$data
=
array
(
'severity'
=>
$this
->
exception
->
severity
(),
'message'
=>
$this
->
exception
->
message
(),
'line'
=>
$this
->
exception
->
getLine
(),
'trace'
=>
$this
->
exception
->
getTraceAsString
(),
'contexts'
=>
$this
->
exception
->
context
(),
);
return
Response
::
make
(
View
::
make
(
'error.exception'
,
$data
),
500
);
}
/**
* Get the generic error report for the exception.
*
* @return Response
*/
private
function
generic_response
()
{
return
Response
::
error
(
'500'
);
}
}
\ No newline at end of file
system/e
rro
r.php
→
system/e
xception/wrappe
r.php
View file @
5c3c2e2d
<?php
namespace
System
;
<?php
namespace
System
\Exception
;
class
Error
{
use
System\File
;
class
Wrapper
{
/**
* The exception being wrapped.
*
* @var Exception
*/
public
$exception
;
/**
/**
* Human-readable error levels and descriptions.
* Human-readable error levels and descriptions.
*
*
* @var array
* @var array
*/
*/
p
ublic
static
$levels
=
array
(
p
rivate
$levels
=
array
(
0
=>
'Error'
,
0
=>
'Error'
,
E_ERROR
=>
'Error'
,
E_ERROR
=>
'Error'
,
E_WARNING
=>
'Warning'
,
E_WARNING
=>
'Warning'
,
...
@@ -24,96 +33,78 @@ class Error {
...
@@ -24,96 +33,78 @@ class Error {
);
);
/**
/**
*
Handle an exception
.
*
Create a new exception wrapper instance
.
*
*
* @param Exception $e
* @param Exception $e
* @return void
* @return void
*/
*/
public
static
function
handle
(
$e
)
public
function
__construct
(
$e
)
{
{
// Clear the output buffer so nothing is sent to the browser except the error
$this
->
exception
=
$e
;
// message. This prevents any views that have already been rendered from being
}
// in an incomplete or erroneous state.
if
(
ob_get_level
()
>
0
)
ob_clean
();
$severity
=
(
array_key_exists
(
$e
->
getCode
(),
static
::
$levels
))
?
static
::
$levels
[
$e
->
getCode
()]
:
$e
->
getCode
();
$message
=
static
::
format
(
$e
);
if
(
Config
::
get
(
'error.log'
))
/**
* Get a human-readable version of the exception error code.
*
* @return string
*/
public
function
severity
()
{
if
(
array_key_exists
(
$this
->
exception
->
getCode
(),
$this
->
levels
))
{
{
call_user_func
(
Config
::
get
(
'error.logger'
),
$severity
,
$message
,
$e
->
getTraceAsString
())
;
return
$this
->
levels
[
$this
->
exception
->
getCode
()]
;
}
}
static
::
show
(
$e
,
$severity
,
$message
);
return
$this
->
exception
->
getCode
();
exit
(
1
);
}
}
/**
/**
* Format the error message for a given exception.
* Get the exception error message formatted for use by Laravel.
*
* The exception file paths will be shortened, and the file name and line number
* will be added to the exception message.
*
*
* @param Exception $e
* @return string
* @return string
*/
*/
p
rivate
static
function
format
(
$e
)
p
ublic
function
message
(
)
{
{
$file
=
str_replace
(
array
(
APP_PATH
,
SYS_PATH
),
array
(
'APP_PATH/'
,
'SYS_PATH/'
),
$
e
->
getFile
());
$file
=
str_replace
(
array
(
APP_PATH
,
SYS_PATH
),
array
(
'APP_PATH/'
,
'SYS_PATH/'
),
$
this
->
exception
->
getFile
());
return
rtrim
(
$
e
->
getMessage
(),
'.'
)
.
' in '
.
$file
.
' on line '
.
$e
->
getLine
()
.
'.'
;
return
rtrim
(
$
this
->
exception
->
getMessage
(),
'.'
)
.
' in '
.
$file
.
' on line '
.
$this
->
exception
->
getLine
()
.
'.'
;
}
}
/**
/**
*
Show the error view
.
*
Get the code surrounding the line where the exception occurred
.
*
*
* @param Exception $e
* @return array
* @param string $severity
* @param string $message
* @return void
*/
*/
p
rivate
static
function
show
(
$e
,
$severity
,
$message
)
p
ublic
function
context
(
)
{
{
if
(
Config
::
get
(
'error.detail'
))
return
File
::
snapshot
(
$this
->
exception
->
getFile
(),
$this
->
exception
->
getLine
());
{
$view
=
View
::
make
(
'error/exception'
)
->
bind
(
'severity'
,
$severity
)
->
bind
(
'message'
,
$message
)
->
bind
(
'line'
,
$e
->
getLine
())
->
bind
(
'trace'
,
$e
->
getTraceAsString
())
->
bind
(
'contexts'
,
static
::
context
(
$e
->
getFile
(),
$e
->
getLine
()));
Response
::
make
(
$view
,
500
)
->
send
();
}
else
{
Response
::
error
(
'500'
)
->
send
();
}
}
}
/**
/**
* Get the code surrounding a given line in a file.
* Magic Method to handle getting properties from the exception.
*
* @param string $path
* @param int $line
* @param int $padding
* @return string
*/
*/
p
rivate
static
function
context
(
$path
,
$line
,
$padding
=
5
)
p
ublic
function
__get
(
$key
)
{
{
if
(
file_exists
(
$path
))
return
$this
->
exception
->
$key
;
{
}
$file
=
file
(
$path
,
FILE_IGNORE_NEW_LINES
);
array_unshift
(
$file
,
''
);
if
((
$start
=
$line
-
$padding
)
<
0
)
$start
=
0
;
if
((
$length
=
(
$line
-
$start
)
+
$padding
+
1
)
<
0
)
$length
=
0
;
return
array_slice
(
$file
,
$start
,
$length
,
true
);
/**
}
* Magic Method to handle setting properties on the exception.
*/
public
function
__set
(
$key
,
$value
)
{
$this
->
exception
->
$key
=
$value
;
}
return
array
();
/**
* Magic Method to pass function calls to the exception.
*/
public
function
__call
(
$method
,
$parameters
)
{
return
call_user_func_array
(
array
(
$this
->
exception
,
$method
),
$parameters
);
}
}
}
}
\ No newline at end of file
system/file.php
View file @
5c3c2e2d
...
@@ -49,6 +49,29 @@ class File {
...
@@ -49,6 +49,29 @@ class File {
}
}
/**
/**
* Get the lines surrounding a given line in a file.
*
* @param string $path
* @param int $line
* @param int $padding
* @return array
*/
public
static
function
snapshot
(
$path
,
$line
,
$padding
=
5
)
{
if
(
!
file_exists
(
$path
))
return
array
();
$file
=
file
(
$path
,
FILE_IGNORE_NEW_LINES
);
array_unshift
(
$file
,
''
);
if
((
$start
=
$line
-
$padding
)
<
0
)
$start
=
0
;
if
((
$length
=
(
$line
-
$start
)
+
$padding
+
1
)
<
0
)
$length
=
0
;
return
array_slice
(
$file
,
$start
,
$length
,
true
);
}
/**
* Get a file MIME type by extension.
* Get a file MIME type by extension.
*
*
* @param string $extension
* @param string $extension
...
...
system/laravel.php
View file @
5c3c2e2d
...
@@ -59,25 +59,27 @@ ini_set('display_errors', 'Off');
...
@@ -59,25 +59,27 @@ ini_set('display_errors', 'Off');
// --------------------------------------------------------------
// --------------------------------------------------------------
set_exception_handler
(
function
(
$e
)
set_exception_handler
(
function
(
$e
)
{
{
require_once
SYS_PATH
.
'e
rro
r'
.
EXT
;
require_once
SYS_PATH
.
'e
xception/handle
r'
.
EXT
;
E
rror
::
handle
(
$e
);
E
xception\Handler
::
make
(
$e
)
->
handle
();
});
});
set_error_handler
(
function
(
$number
,
$error
,
$file
,
$line
)
set_error_handler
(
function
(
$number
,
$error
,
$file
,
$line
)
{
{
require_once
SYS_PATH
.
'e
rro
r'
.
EXT
;
require_once
SYS_PATH
.
'e
xception/handle
r'
.
EXT
;
E
rror
::
handle
(
new
\ErrorException
(
$error
,
$number
,
0
,
$file
,
$line
)
);
E
xception\Handler
::
make
(
new
\ErrorException
(
$error
,
$number
,
0
,
$file
,
$line
))
->
handle
(
);
});
});
register_shutdown_function
(
function
()
register_shutdown_function
(
function
()
{
{
if
(
!
is_null
(
$error
=
error_get_last
()))
if
(
!
is_null
(
$error
=
error_get_last
()))
{
{
require_once
SYS_PATH
.
'error'
.
EXT
;
require_once
SYS_PATH
.
'exception/handler'
.
EXT
;
Error
::
handle
(
new
\ErrorException
(
$error
[
'message'
],
$error
[
'type'
],
0
,
$error
[
'file'
],
$error
[
'line'
]));
extract
(
$error
);
Exception\Handler
::
make
(
new
\ErrorException
(
$message
,
$type
,
0
,
$file
,
$line
))
->
handle
();
}
}
});
});
...
...
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