Introduction
errors helps manage errors and exceptions in PHP applications. Part of this library is based on the excellent work in the Haldayne\Fox project.
Usage
Let’s say you want to use PHP’s touch function. This function return false and emit an E_WARNING if the file can not be created. A way to workaround this behavior is to use the @ operator which is considered to be a bad practice as it silenced error reporting and slow down PHP execution. The Carpediem\Errors library helps you better handle these limitations gradually.
<?php
$result = touch('/foo/bar');
//if you don't have access to '/foo' directory
// $result = false
// an E_WARNING is emitted with a associated message
If you want to capture the error from the touch function.
<?php
use Carpediem\Errors\CaptureError;
$touch = new CaptureError('touch');
$result = $touch('/foo/bar');
if (!$result) {
throw new RuntimeException($touch->getLastErrorMessage(), $touch->getLastErrorCode());
}
If you want to convert the error from the touch function into an Exception.
<?php
use Carpediem\Errors\CaptureError;
use Carpediem\Errors\ErrorToException;
$touch = new ErrorToException(new CaptureError('touch'));
try {
$result = $touch('/foo/bar');
} catch (Exception $e) {
echo $e->getMessage(); // the same message as CaptureError::getLastErrorMessage
echo $e->getCode(); // the same message as CaptureError::getLastErrorCode
}
Credits
License
The MIT License (MIT). Please see LICENSE for more information.