ErrorHandler

Type
Class
Namespace
Craft
Inherits
Craft\ErrorHandler » CErrorHandler » CApplicationComponent » CComponent
Implements
IApplicationComponent
Since
1.0

ErrorHandler handles uncaught PHP errors and exceptions.

It displays these errors using appropriate views based on the nature of the error and the mode the application runs at. It also chooses the most preferred language for displaying the error.

ErrorHandler uses two sets of views:

  • development templates, named as exception.php;
  • production templates, named as error<StatusCode>.php;

where <StatusCode> stands for the HTTP error code (e.g. error500.php). Localized templates are named similarly but located under a subdirectory whose name is the language code (e.g. zh_cn/error500.php).

Development templates are displayed when the application is in dev mode (i.e. craft()->config->get('devMode') = true). Detailed error information with source code are displayed in these templates. Production templates are meant to be shown to end-users and are used when the application is in production mode. For security reasons, they only display the error message without any sensitive information.

ErrorHandler looks for the templates from the following locations in order:

  • craft/templates/{siteHandle}/errors: when a theme is active.
  • craft/app/templates/errors
  • craft/app/framework/views

If the template is not found in a directory, it will be looked for in the next directory. The property {@link maxSourceLines} can be changed to specify the number of source code lines to be displayed in development views.

ErrorHandler is a core application component that can be accessed via {@link \CApplication::getErrorHandler()}.

See also http://craftcms.com

View source

Public Properties

PropertyDescription
$adminInfostring – The application administrator information (could be a name or email link).
$behaviorsarray – The behaviors that should be attached to this component.
$discardOutputboolean – Whether to discard any existing page output before error display.
$errorActionstring – The route (eg 'site/error') to the controller action that will be used to display external errors.
$maxSourceLinesinteger – Maximum number of source code lines to be displayed.
$maxTraceSourceLinesinteger – Maximum number of trace source code lines to be displayed.

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__get()Returns a property value, an event handler list or a behavior based on its name.
__isset()Checks if a property value is null.
__set()Sets value of a component property.
__unset()Sets a component property to be null.
asa()Returns the named behavior object.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
attachEventHandler()Attaches an event handler to an event.
canGetProperty()Determines whether a property can be read.
canSetProperty()Determines whether a property can be set.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
detachEventHandler()Detaches an existing event handler.
disableBehavior()Disables an attached behavior.
disableBehaviors()Disables all behaviors attached to this component.
enableBehavior()Enables an attached behavior.
enableBehaviors()Enables all behaviors attached to this component.
evaluateExpression()Evaluates a PHP expression or callback under the context of this component.
getError()Returns the stored error, if there is one.
getEventHandlers()Returns the list of attached event handlers for an event.
getException()Returns the instance of the exception that is currently being handled.
getIsInitialized()Checks if this application component has been initialized.
handle()Handles the exception/error event.
hasEvent()Determines whether an event is defined.
hasEventHandler()Checks whether the named event has attached handlers.
hasProperty()Determines whether a property is defined.
init()Initializes the application component.
logException()Logs an exception in the same way that {@link \CWebApplication::handleException()} does.
raiseEvent()Raises an event.

getError()

Returns the stored error, if there is one.

View source

Returns

array, null

Signature

public array, null getError ( )

logException()

Logs an exception in the same way that {@link \CWebApplication::handleException()} does.

View source

Arguments

  • $exception (Exception) – The exception that should be logged

Returns

void

Signature

public void logException ( Exception $exception )

Protected Methods

MethodDescription
argumentsToString()Converts arguments array to its string representation
getExactTrace()Returns the exact trace where the problem occurs.
getHttpHeader()Return correct message for each known http error code
getVersionInfo()Returns server version information. If the site is in non-dev mode, an empty string is returned.
getViewFile()Determines which view file should be used.
getViewFileInternal()Looks for the view under the specified directory.
handleDbConnectionError()Handles DB connection errors.
handleError()Handles a PHP error.
handleException()Handles a thrown exception. Will also log extra information if the exception happens to by a MySql deadlock.
handleTwigError()Handles Twig syntax errors.
isAjaxRequest()Whether the current request is an AJAX (XMLHttpRequest) request.
isCoreCode()Returns a value indicating whether the call stack is from application code.
render()Renders the view.
renderError()Renders the exception information. This method will display information from current {@link error} value.
renderException()Renders the exception information.
renderSourceCode()Renders the source code around the error line.

getVersionInfo()

Returns server version information. If the site is in non-dev mode, an empty string is returned.

View source

Returns

string – The server version information. Empty if in non-dev mode.

Signature

protected string getVersionInfo ( )

handleDbConnectionError()

Handles DB connection errors.

View source

Arguments

Returns

null

Signature

protected null handleDbConnectionError ( Craft\DbConnectException $exception )

handleError()

Handles a PHP error.

View source

Arguments

Returns

null

Signature

protected null handleError ( $event )

handleException()

Handles a thrown exception. Will also log extra information if the exception happens to by a MySql deadlock.

View source

Arguments

  • $exception (Exception) – The exception captured.

Returns

null

Signature

protected null handleException ( $exception )

handleTwigError()

Handles Twig syntax errors.

View source

Arguments

  • $exception (\Twig_Error)

Returns

null

Signature

protected null handleTwigError ( \Twig_Error $exception )

renderError()

Renders the exception information. This method will display information from current {@link error} value.

View source

Signature

protected void renderError ( )