-
-
Save AmyStephen/4163323 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* @package Molajo | |
* @copyright 2012 Individual Molajo Contributors. All rights reserved. | |
* @license GNU GPL v 2, or later and MIT, see License folder | |
*/ | |
namespace Molajo\Service\Services\Exception; | |
use Molajo\Service\Services; | |
use \Exception; | |
defined('MOLAJO') or die; | |
/** | |
* Exception | |
* | |
* @package Molajo | |
* @subpackage Service | |
* @since 1.0 | |
*/ | |
Class ExceptionService extends \Exception | |
{ | |
/** | |
* Class construct | |
* | |
* @param string $title | |
* @param string $message | |
* @param int $code | |
* @param \Exception $previous | |
* | |
* @return void | |
* @since 1.0 | |
*/ | |
public function __construct($message = '', $code = 0, Exception $previous = null) | |
{ | |
parent::__construct($message, $code, $previous); | |
} | |
/** | |
* Format Custom Message | |
* | |
* @return string | |
* @since 1.0 | |
*/ | |
public function formatMessage( | |
$title = null, | |
$message = null, | |
$code = null, | |
$display_file = 1, | |
$display_line = 1, | |
$display_stack_trace = 1, | |
$terminate = 1 | |
) { | |
if ($title === null) { | |
$title = 'Molajo Exception Information'; | |
} | |
if ($message === null) { | |
$message = $this->getMessage(); | |
} | |
if ($code === null) { | |
$code = $this->getCode(); | |
} | |
$error_message = ''; | |
$error_message .= '<strong>' . $title . '</strong> ' . '<br />'; | |
$error_message .= '<strong>Date: </strong>' . date('M d, Y h:iA') . '<br />'; | |
$error_message .= '<strong>Message: </strong>' . $message . '<br />'; | |
if ($code === null) { | |
} else { | |
$error_message .= '<strong>Code: </strong>' . $this->getCode() . '<br />'; | |
} | |
if ($display_file == 1) { | |
$error_message .= '<strong>File: </strong>' . $this->getFile() . '<br />'; | |
} | |
if ($display_line == 1) { | |
$error_message .= '<strong>Line: </strong>' . $this->getLine() . '<br />'; | |
} | |
ob_start(); | |
echo $error_message; | |
if ($display_stack_trace == 1) { | |
echo '<strong>Stack Trace: </strong><br />'; | |
echo '<pre>'; | |
echo $this->getTraceAsString(); | |
echo '</pre>'; | |
} | |
if ($terminate == 1) { | |
echo 'Application will now terminate.'; | |
} | |
$renderedOutput = ob_get_contents(); | |
ob_end_clean(); | |
echo $renderedOutput; | |
if ($terminate == 1) { | |
die; | |
} | |
} | |
} |
Okay, so it looks like the method you pass into set_exception_handler() should only take one argument: an Exception object. See http://us3.php.net/manual/en/function.set-exception-handler.php
I am guessing that you'll take in that exception, extract the data you want via methods and properties of the exception, and pass those to your formatter. Or let the formatter handle it all. See details here: http://us3.php.net/manual/en/class.exception.php
further, I'm guessing all the data appears to come in $title as a string because the Exception is getting cast as a string when you try to do stringy stuff to it. like if you just do echo $exception_object
it will output data about the exception as a string.
You are absolutely right, I read that page a hundred times, if I read it once. Initially, I had it that way, then I was working on getting the stack trace to display the correct sequence (instead of the trace from the handler to my Exception class - and I ended up introducing another problem.
I was looking around and I see Fabien has a post that addresses the stack trace issue. http://fabien.potencier.org/article/9/php-serialization-stack-traces-and-exceptions
Example of what I wind up with when I throw an exception:
exception 'Exception' with message 'onAfterRouteEvent Failed' in /Users/amystephen/Sites/MolajoStandard/Vendor/Molajo/Application.php:407 Stack trace: #0 /Users/amystephen/Sites/MolajoStandard/Vendor/Molajo/Application.php(346): Molajo\Application->onAfterRouteEvent() #1 /Users/amystephen/Sites/MolajoStandard/Vendor/Molajo/Application.php(122): Molajo\Application->route() #2 /Users/amystephen/Sites/MolajoStandard/index.php(37): Molajo\Application->process(false, false, false, false) #3 {main} Next exception 'Exception' with message 'Route Error: onAfterRouteEvent Failed' in /Users/amystephen/Sites/MolajoStandard/Vendor/Molajo/Application.php:125 Stack trace: #0 /Users/amystephen/Sites/MolajoStandard/index.php(37): Molajo\Application->process(false, false, false, false) #1 {main}
Date: Nov 28, 2012 01:03PM
Message:
Code: 0
File: /Users/amystephen/Sites/MolajoStandard/Vendor/Molajo/Application.php
Line: 289
Stack Trace:
#0 [internal function]: Molajo\Application->handleException(Object(Exception))
#1 {main}
Application will now terminate.