This class provides a static method for attempting to run a closure and capturing the result or any exception that is thrown.
Closure
$closure: The closure to be attempted.
AttemptEntity
: An instance ofAttemptEntity
containing the result of the closure, or the exception that was- thrown during the attempt.
use Midnite81\Core\Helpers\Attempt;
$result = Attempt::attempt(function () {
return 'Hello World';
});
if ($result->successful) {
echo $result->result;
}
if ($result->hasErrored) {
echo $result->throwable->getMessage();
}