RazorErrorHandler

Errors in razorCMS are handled slightly different to normal error handling, the main difference being the masking of errors from the outside world. When encountering an error in the system, we mask the error when being run as a production server to stop 3rd parties being able to gather any kind of information on the system, whilst storing this information in a log. On a production system, the only error data you will encounter will be stored in the /storage/logs directory with the default log file for the system being razor_error_log.php (a php commented file to ensure privacy from 3rd parties)

When running the system locally, razorCMS tries to capture this as best as possible, switching the system to development mode. Once in development mode, razorCMS will log all errors as before, but will also display them to the user. In the case of performing AJAX requests to a rars model, the request will also be logged to chromePHP; if you have chromePHP extension installed in chrome, and turned on, you will also see errors posted in console (F12 developer mode).

It is possible to trigger errors in your code using the trigger_error() PHP function, these errors will also be pushed through the razor error handler.

Handling Exceptions

At present there is no means to extend exception handling, all exceptions are pushed through the same error handler, any exceptions thrown will be logged and will show in the log as an exception thrown. Any information thrown in with the exception will be logged against the record made.

In the case of handling exceptions from rars model files, throwing an exception will result in a response being output/returned to the requester, along with correct HTML code and headers, as well as logging the issue to the log file. Any code after an exception is thrown will not be run.

Debugging

To aid debugging of AJAX requests, it is also possible to log data to the browsers console using chromePHP.

PHP chrome_php(data, debug)

Output data to the chrome console (F12 Developer Tools). Thi srequires the installation of the chromePHP extension in your chrome browser, please search for chromePHP in the chrome store, install and activate.

$obj->chrome_php("hello", true)

Parameters

  • data mixed The data to output to the console, this can be any type of data.
  • debug bool The debug flag must be set to true to force debug mode.

Example

// include class if not loaded (error handler should always be loaded)
include_once(RAZOR_BASE_PATH.'library/php/razor/razor_error_handler.php');

// create new razor error handler object
$debug = new RazorErrorHandler();

// result - if chrome PHP is turned on, your browser console should display 'Hello'
$debug->chrome_php("Hello!", true);

Logging

You may also log anything you like to the error log or an optional log of your choice (in the same directory).

PHP log_error(error, [log_book])

Write a log manually to the default razor_error_log or another log book of your choice (in the same directory).

$obj->log_error($error, "type-handle-extension-log")

Parameters

  • error array The error array, containing all the error data.
  • error[error] string The error you are throwing, your error name.
  • error[type] string optionalThe error type, you could give it a code or group.
  • error[file] string optionalThe error file, the file in which the error was triggered.
  • error[line] string optionalThe error array, the line the error is triggered on.
  • error[string] string The error message to output.
  • log_book string optional The name of the log book, [a-zA-Z0-9_-] defaults to 'razor-error-log'. When setting a new log book, please try to keep to some basic principles. You will only ever need to do this from extensions as the system logs system errors, keep additional log books to the convention ['extension type'_'your razor user handle'_'extension name'].

Example

// include class if not loaded (error handler should always be loaded)
include_once(RAZOR_BASE_PATH.'library/php/razor/razor_error_handler.php');
include_once(RAZOR_BASE_PATH.'library/php/razor/razor_api.php');

// create new razor error handler object
$logger = new RazorErrorHandler();

// result - the log will be written to your log file, then a response returned to requestor.
$logger->log_error(["error" => "My Bad Error", "string" => "Something bad happened"], 'type-handle-extension-log');
RazorAPI::response(null, null, 500);