ErrorHandler
in package
Uses
BackwardCompatibility
SMF's error handler.
Also provides methods for logging and/or dying when errors occur.
Table of Contents
Properties
- $backcompat : array<string|int, mixed>
Methods
- __construct() : mixed
- Constructor.
- call() : void
- Convenience method to create an instance of this class.
- displayDbError() : void
- Show an error message for the connection problems.
- displayLoadAvgError() : void
- Show an error message for load average blocking problems.
- displayMaintenanceMessage() : void
- Show a message for the (full block) maintenance mode.
- exportStatic() : void
- Provides a way to export a class's public static properties and methods to global namespace.
- fatal() : void
- An irrecoverable error.
- fatalLang() : mixed
- Shows a fatal error with a message stored in the language file.
- log() : string
- Log an error, if the error logging is enabled.
- logLastDatabaseError() : mixed
- Logs the last database error into a file.
- logOnline() : mixed
- Small utility function for fatal error pages.
- setFatalHeaders() : void
- Small utility function for fatal error pages.
- setupFatalContext() : void
- It is called by self::fatal() and self::fatalLang().
Properties
$backcompat
private
static array<string|int, mixed>
$backcompat
= ['func_names' => ['call' => 'smf_error_handler', 'log' => 'log_error', 'fatal' => 'fatal_error', 'fatalLang' => 'fatal_lang_error', 'displayMaintenanceMessage' => 'display_maintenance_message', 'displayDbError' => 'display_db_error', 'displayLoadAvgError' => 'display_loadavg_error']]
BackwardCompatibility settings for this class.
Methods
__construct()
Constructor.
public
__construct(int $error_level, string $error_string, string $file, int $line) : mixed
Parameters
call()
Convenience method to create an instance of this class.
public
static call(int $error_level, string $error_string, string $file, int $line) : void
Parameters
displayDbError()
Show an error message for the connection problems.
public
static displayDbError() : void
It shows a complete page independent of language files or themes. It is used only if there's no way to connect to the database. It stops further execution of the script.
displayLoadAvgError()
Show an error message for load average blocking problems.
public
static displayLoadAvgError() : void
It shows a complete page independent of language files or themes. It is used only if the load averages are too high to continue execution. It stops further execution of the script.
displayMaintenanceMessage()
Show a message for the (full block) maintenance mode.
public
static displayMaintenanceMessage() : void
It shows a complete page independent of language files or themes. It is used only if $maintenance = 2 in Settings.php. It stops further execution of the script.
exportStatic()
Provides a way to export a class's public static properties and methods to global namespace.
public
static exportStatic() : void
To do so:
- Use this trait in the class.
- At the END of the class's file, call its exportStatic() method.
Although it might not seem that way at first glance, this approach conforms to section 2.3 of PSR 1, since executing this method is simply a dynamic means of declaring functions when the file is included; it has no other side effects.
Regarding the $backcompat items:
A class's static properties are not exported to global variables unless explicitly included in $backcompat['prop_names']. Likewise, a class's static methods are not exported as global functions unless explicitly included in $backcompat['func_names'].
$backcompat['prop_names'] is a simple array where the keys are the names of one or more of a class's static properties, and the values are the names of global variables. In each case, the global variable will be set to a reference to the static property. Static properties that are not named in this array will not be exported.
$backcompat['func_names'] is a simple array where the keys are the names of one or more of a class's static methods, and the values are the names that should be used for global functions that will encapsulate those methods. Methods that are not named in this array will not be exported.
Adding non-static properties or methods to the $backcompat arrays will produce runtime errors. It is the responsibility of the developer to make sure not to do this.
fatal()
An irrecoverable error.
public
static fatal(string $error[, string|bool $log = 'general' ][, int $status = 500 ]) : void
This function stops execution and displays an error message. It logs the error message if $log is specified.
Parameters
- $error : string
-
The error message
- $log : string|bool = 'general'
-
= 'general' What type of error to log this as (false to not log it))
- $status : int = 500
-
The HTTP status code associated with this error
fatalLang()
Shows a fatal error with a message stored in the language file.
public
static fatalLang(string $error[, string|false $log = 'general' ][, array<string|int, mixed> $sprintf = [] ][, int $status = 403 ]) : mixed
This function stops execution and displays an error message by key.
- uses the string with the error_message_key key.
- logs the error in the forum's default language while displaying the error message in the user's language.
- uses Errors language file and applies the $sprintf information if specified.
- the information is logged if log is specified.
Parameters
- $error : string
-
The error message.
- $log : string|false = 'general'
-
The type of error, or false to not log it.
- $sprintf : array<string|int, mixed> = []
-
An array of data to be sprintf()'d into the specified message.
- $status : int = 403
-
The HTTP status code associated with this error. Default: 403.
log()
Log an error, if the error logging is enabled.
public
static log(string $error_message[, string|bool $error_type = 'general' ][, string $file = '' ][, int $line = 0 ]) : string
$file and $line should be FILE and LINE, respectively.
Example use: die(ErrorHandler::log($msg));
Parameters
- $error_message : string
-
The message to log.
- $error_type : string|bool = 'general'
-
The type of error.
- $file : string = ''
-
The name of the file where this error occurred.
- $line : int = 0
-
The line where the error occurred.
Return values
string —The message that was logged.
logLastDatabaseError()
Logs the last database error into a file.
protected
static logLastDatabaseError() : mixed
Attempts to use the backup file first, to store the last database error and only update db_last_error.php if the first was successful.
logOnline()
Small utility function for fatal error pages.
protected
static logOnline(string $error[, array<string|int, mixed> $sprintf = [] ]) : mixed
Used by self::fatal() and self::fatalLang().
Parameters
- $error : string
-
The error
- $sprintf : array<string|int, mixed> = []
-
An array of data to be sprintf()'d into the specified message
setFatalHeaders()
Small utility function for fatal error pages.
protected
static setFatalHeaders() : void
Used by self::displayMaintenanceMessage(), self::displayDbError(), and self::displayLoadAvgError().
setupFatalContext()
It is called by self::fatal() and self::fatalLang().
protected
static setupFatalContext(string $error_message[, string $error_code = null ]) : void
Parameters
- $error_message : string
-
The error message
- $error_code : string = null
-
An error code