cleancookie/attempt

Attempt helper function

dev-main 2024-11-14 09:14 UTC

This package is auto-updated.

Last update: 2025-06-14 17:56:25 UTC


README

A simple function to attempt a function inspired by idiomatic error handling in Golang.

Why?

I've ran into countless situations where I have to move a variable outside of a try catch just because it is in a different scope

// ❌ $success is needed across difference scopes.  Also as an aside $success mutated on different lines
$success = null;
try {
    writeToFile();
    $success = true;
} catch (Exception $e) {
    $success = false;
}

if (!$success) {
    // ...
}
// ✅ With attempt, I can write code top to bottom, saving on the whiplash
[$error, $value] = attempt(fn() => writeToFile());

$success = !$error;

It's also nice to just have one less layer of indentation sometimes

// 1 layer of indentation 🙅
try {
    $value = someFunction();
    logger()->debug($value);
} catch (Exception $e) {
    report($e);
}
// 0 layers of indentation 👌
[$error, $value] = attempt(fn() => someFunction());

Installation

composer require cleancookie/attempt

And may your functions be attempted

Usage

[$error, $value] = attempt(fn() => throw new Exception('This is an exception'));

if ($error) {
    throw $error; // The reported error will still point to the original exception
}

echo $value;

You can also check for specific exception types:

[$error, $value] = attempt(fn() => throw new Exception('This is an exception'));

if ($error instanceof \Exception) {
    throw $error;
}

echo $value;

You can also ignore errors if you don't care about them:

[$_, $value] = attempt(fn() => throw new Exception('This is an exception'));

echo $value;