Skip to content

Libraries Exceptional

Jeff Johns edited this page Mar 4, 2014 · 2 revisions
Library Extends Path
Exceptional N/A /application/libraries/Exceptional.php

This library is used to log errors and exceptions. It will also allow you to add your own custom error tracking library if you wish. You can read more about that here.

Methods

__construct - Public

Called automatically at object creation and does the following:

  • Sets the handlers for all exceptions and errors for PHP

createTrace - Public

Will attempt to create/correct the backtrace for you. If the type argument is not set below, the method will call error_get_last to obtain the error type, message, file and number line number for you. This is only helpful for PHP errors. If you already have the exception object from a valid exception please use the logException method. This method is also automatically called with PHP errors, notices and warnings. You can manually log items using the example below.

This method will automatically send the information compiled to Airbrake for you if it can.

Arguments

Variable Type Default Required Description
$type string null No The error type constant.
$message string null No The message to set with the backtrace.
$file string null No The file that errored.
$line string null No The line number.
$custom_data array array() No Any custom data (key/value) pairs to send with the request.

Example

$this->exceptional->createTrace(E_ERROR, 'Error Message', __FILE__, __LINE__, array(
    'info_1' => print_r($info, true),
    'info_2' => $token
));

logException - Public

Will take the exception object passed, correct the trace object and send it to Airbrake to be processed. This method is also automatically called from PHP when it can be. You can also call it yourself using the example below.

Arguments

Variable Type Default Required Description
$e object N/A Yes The complete exception object provided by PHP.

Example

try {
    // do something
}
catch ($e) {
    $this->exceptional->logException($e);
}

sendToTracker - Public

Sends the information to your custom error tracking class or simply logs to the default CodeIgniter error log.

Arguments

Variable Type Default Required Description
$message string N/A Yes The general message to send to airbrake about this exception.
$type string N/A Yes The type of exception (error, exception, notice, etc).
$backtrace object or array N/A Yes The trace object from the request.
$custom_data array array() No Any custom data to send wtih the request (key/values).
$exception_object object false No The raw exception object. If passed it will be passed on to any custom tracking class.

Example

$this->sendToTracker('Exception: ' . $e->getMessage(), 'Exception', $trace);

setHandlers - Public

Simply sets the exception handler for PHP and the register shutdown function for fatal errors. CodeIgniter already sets the error handler for us.

Example

$this->setHandlers();

// The code
set_exception_handler(array($this, 'logException'));
register_shutdown_function(array($this, 'createTrace'));