Class: ErrorHandler

Class for error handling.

Implements

Authors

Oleg Schildt

Package

Source code

 

Properties
static protected

Internal variable for storing the last error.

protected

Internal variable for storing the log path.

static protected

Internal variable for storing the state of tracing - active or not.

Methods
protected

This is an auxiliary function for generation of the detailed string from the function arguments from the standard PHP backtrace (debug_backtrace).

public

Disables the trace.

public

Enables the trace.

protected

Extracts the call stack from the standard PHP backtrace (debug_backtrace).

public

Formats the standard PHP backtrace (debug_backtrace).

public

Returns the last error.

public

This is the function for handling of the PHP errors. It is set in the error handler.

public

This is the function for handling of the PHP exceptions. It should be called in the catch block to trace detailed information if an exception is thrown.

public

Initializes the error handler with parameters.

protected

Generates the detailed string from the function arguments from the standard PHP backtrace (debug_backtrace).

public

Stores the last error.

public

Returns the state whether the trace is active or not.

protected

Traces an error to the standard trace file (logs/trace.log).

protected

This is an auxiliary function that cuts off the common part of the path.

protected

This is an auxiliary function for truncating long arguments.


Property: ErrorHandler::$last_error

protected static string $last_error = "";

Internal variable for storing the last error.

Type

string

Authors

Oleg Schildt

Source code

See also


Property: ErrorHandler::$log_path

protected ?string $log_path = null;

Internal variable for storing the log path.

Type

?string

Authors

Oleg Schildt

Source code


Property: ErrorHandler::$trace_disabled

protected static bool $trace_disabled = false;

Internal variable for storing the state of tracing - active or not.

Type

bool

Authors

Oleg Schildt

Source code

See also


Method: ErrorHandler::deep_implode()

protected function deep_implode(array &$arr): string;

This is an auxiliary function for generation of the detailed string from the function arguments from the standard PHP backtrace (debug_backtrace).

Parameters

Name Pass type Value type Default value Description
$arr by reference array

The array of the function arguments.

Returns

string

Returns the detailed string from the function arguments.

Authors

Oleg Schildt

Source code


Method: ErrorHandler::disableTrace()

public function disableTrace(): void;

Disables the trace.

Details

If the trace is active, any error, warning or notice is traced to the standard file.

Returns

void

Overrides

Authors

Oleg Schildt

Source code

See also


Method: ErrorHandler::enableTrace()

public function enableTrace(): void;

Enables the trace.

Details

If the trace is active, any error, warning or notice is traced to the standard file.

Returns

void

Overrides

Authors

Oleg Schildt

Source code

See also


Method: ErrorHandler::extract_call_stack()

protected function extract_call_stack(array $btrace): string;

Extracts the call stack from the standard PHP backtrace (debug_backtrace).

Parameters

Name Pass type Value type Default value Description
$btrace by value array

The backtrace.

Returns

string

Returns the extracted call stack.

Authors

Oleg Schildt

Source code


Method: ErrorHandler::format_backtrace()

function format_backtrace(array $btrace): string;

Formats the standard PHP backtrace (debug_backtrace).

Parameters

Name Pass type Value type Default value Description
$btrace by value array

The backtrace.

Returns

string

Returns the formatted backtrace.

Authors

Oleg Schildt

Source code


Method: ErrorHandler::getLastError()

public function getLastError(): string;

Returns the last error.

Returns

string

Returns the last error or an empty string if no error occurred so far.

Overrides

Authors

Oleg Schildt

Source code


Method: ErrorHandler::handleError()

public function handleError(int $errno, string $errstr, string $errfile, int $errline): void;

This is the function for handling of the PHP errors. It is set in the error handler.

Parameters

Name Pass type Value type Default value Description
$errno by value int

Error code.

$errstr by value string

Error text.

$errfile by value string

Source file where the error occurred.

$errline by value int

Line number where the error occurred.

Returns

void

Throws

\Exception

It might throw an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: ErrorHandler::handleException()

public function handleException(\Throwable $ex, int $errno): void;

This is the function for handling of the PHP exceptions. It should be called in the catch block to trace detailed information if an exception is thrown.

Parameters

Name Pass type Value type Default value Description
$ex by value \Throwable

Thrown exception.

$errno by value int

Error code.

Returns

void

Throws

\Exception

It might throw an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: ErrorHandler::init()

public function init(array $parameters): void;

Initializes the error handler with parameters.

Parameters

Name Pass type Value type Default value Description
$parameters by value array

Settings for logging as an associative array in the form key => value:

  • $parameters["log_path"] - the target file path where the logs should be stored.

Returns

void

Throws

\Exception

It might throw an exception in the case of any errors:

  • if the log path is not specified.
  • if the trace file is not writable.

Overrides

Authors

Oleg Schildt

Source code


Method: ErrorHandler::make_arg_list()

protected function make_arg_list(array $args): string;

Generates the detailed string from the function arguments from the standard PHP backtrace (debug_backtrace).

Parameters

Name Pass type Value type Default value Description
$args by value array

The array of the function arguments.

Returns

string

Returns the detailed string from the function arguments.

Authors

Oleg Schildt

Source code


Method: ErrorHandler::setLastError()

public function setLastError(string $error): void;

Stores the last error.

Parameters

Name Pass type Value type Default value Description
$error by value string

The error text to be stored.

Returns

void

Overrides

Authors

Oleg Schildt

Source code


Method: ErrorHandler::traceActive()

public function traceActive(): bool;

Returns the state whether the trace is active or not.

Details

If the trace is active, any error, warning or notice is traced to the standard file.

The trace is generally managed over the setting tracing_enabled. But you can also temporarily disable tracing, e.g. to keep the trace log clear when you make a check that can produce a trace entry, but it is a controlled noticed and should not clutter the trace.

Returns

bool

Returns the state whether the trace is active or not.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: ErrorHandler::trace_message()

protected function trace_message(string $etype, string $message): bool;

Traces an error to the standard trace file (logs/trace.log).

Parameters

Name Pass type Value type Default value Description
$etype by value string

The type of the message to be traced.

$message by value string

The message to be traced.

Returns

bool

Returns true if the message has been successfully trace, otherwise false.

Throws

\Exception

It might throw an exception in the case of any errors:

  • if the trace file is not writable.

Authors

Oleg Schildt

Source code


Method: ErrorHandler::trim_path()

protected function trim_path(string $path): string;

This is an auxiliary function that cuts off the common part of the path.

Parameters

Name Pass type Value type Default value Description
$path by value string

The path.

Returns

string

Returns the cut path.

Authors

Oleg Schildt

Source code


Method: ErrorHandler::truncate_argument()

protected function truncate_argument(?string $str, int $limit): string;

This is an auxiliary function for truncating long arguments.

Parameters

Name Pass type Value type Default value Description
$str by value ?string

The string to be truncated.

$limit by value int

The limit of the length after which the string is truncated.

Returns

string

Returns the truncated string.

Authors

Oleg Schildt

Source code