maximaster/exceptior

Functions collection to ease exception handling.

v1.1.1 2024-09-03 08:59 UTC

This package is auto-updated.

Last update: 2024-11-03 09:18:55 UTC


README

Functions collection to ease exception handling.

composer require maximaster/exceptior

CASE #1: convert throwing void function into boolean

use Maximaster\Exceptior\Ex;

$noOpeation = function(): void {};
$wouldThrow = fn () => throw new Exception('hello');

Ex::boolize($noOpeation); // === true
Ex::boolize($wouldThrow); // === false

CASE #2: convert throwing function with return into returning value of your choice

use Maximaster\Exceptior\Ex;

$giveString = fn () => 'hello'
$wouldThrow = fn () => throw new Exception('no hello');

Ex::suppressInto($giveString, 'fail'); // === 'hello'
Ex::suppressInto($wouldThrow, 'fail'); // === 'fail'

CASE #3: convert exception into another exception

use Maximaster\Exceptior\Ex;

$giveString = fn () => 'hello'
$wouldThrow = fn () => throw new Exception('no hello');
$converter = fn (Throwable $thrown) => new RuntimeException($thrown->getMessage());

Ex::convert($giveString, $converter); // === 'hello'
Ex::convert($wouldThrow, $converter); // throws RuntimeException

CASE #4: normalize thrown exception to value provided by callable

use Maximaster\Exceptior\Ex;

$giveString = fn () => 'hello'
$wouldThrow = fn () => throw new Exception('no hello');
$normaizeToMessage = fn (Throwable $throwable) => $throwable->getMessage();

Ex::normalize($giveString, $normaizeToMessage); // === 'hello'
Ex::normalize($wouldThrow, $normaizeToMessage); // === 'no hello'

CASE #5: return worker value or null if worker throws an exception

$giveString = fn () => 'hello'
$wouldThrow = fn () => throw new Exception('no hello');

Ex::nullize($giveString); // === 'hello'
Ex::nullize($wouldThrow); // === null

Development

Fork → Edit → composer check → PR.