Skip to content

Instantly share code, notes, and snippets.

@AmyStephen
Created November 28, 2012 19:05
Show Gist options
  • Save AmyStephen/4163323 to your computer and use it in GitHub Desktop.
Save AmyStephen/4163323 to your computer and use it in GitHub Desktop.
Custom Exception Handler
<?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;
}
}
}
@AmyStephen
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment