Page MenuHomePhorge

final class PhutilErrorTrap
libphutil Technical Documentation (Errors)

Trap PHP errors while this object is alive, so they can be accessed and included in exceptions or other types of logging. For example, if you have code like this:

$res = proc_open(...);

There is no easy way to throw an informative exception if the proc_open() fails. In some cases you may be able to use error_get_last(), but this is unreliable (if proc_open() fails because disable_functions is set, it does not capture the error) and can not capture more than one error.

You can trap errors while executing this code instead:

$trap = new PhutilErrorTrap();
  $res = proc_open(...);
  $err = $trap->getErrorsAsString();
$trap->destroy();

if (!$res) {
  throw new Exception('proc_open() failed: '.$err);
}
IMPORTANT: You must explicitly destroy traps because they register themselves with PhutilErrorHandler, and thus will not be destroyed when unset().

Some notes on traps:

  • Traps catch all errors, including those silenced by @.
  • Traps do not prevent errors from reaching other standard handlers. You can use @ to keep errors out of the logs while still trapping them.
  • Traps capture all errors until they are explicitly destroyed. This means that you should not create long-lived traps, or they may consume unbounded amounts of memory to hold the error log.

Methods

public function __get($name)
Inherited

This method is not documented.
Parameters
$name
Return
wild

public function __set($name, $value)
Inherited

This method is not documented.
Parameters
$name
$value
Return
wild

public function current()
Inherited

This method is not documented.
Return
wild

public function key()
Inherited

This method is not documented.
Return
wild

public function next()
Inherited

This method is not documented.
Return
wild

public function rewind()
Inherited

This method is not documented.
Return
wild

public function valid()
Inherited

This method is not documented.
Return
wild

private function throwOnAttemptedIteration()
Inherited

This method is not documented.
Return
wild

public function getPhobjectClassConstant($key, $byte_limit)
Inherited

Phobject

Read the value of a class constant.

This is the same as just typing self::CONSTANTNAME, but throws a more useful message if the constant is not defined and allows the constant to be limited to a maximum length.

Parameters
string$keyName of the constant.
int|null$byte_limitMaximum number of bytes permitted in the value.
Return
stringValue of the constant.

public function addError($num, $str, $file, $line)

This method is not documented.
Parameters
$num
$str
$file
$line
Return
wild

public function getErrorsAsString()

This method is not documented.
Return
wild

public function destroy()

This method is not documented.
Return
wild

public function getTrapKey()

This method is not documented.
Return
wild

public function __construct()

This method is not documented.
Return
this//Implicit.//

public function __toString()

This method is not documented.
Return
wild