PHPackages                             might-fail/might-fail - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Debugging &amp; Profiling](/categories/debugging)
4. /
5. might-fail/might-fail

ActiveLibrary[Debugging &amp; Profiling](/categories/debugging)

might-fail/might-fail
=====================

A PHP implementation of the MightFail project

0.1.0(1y ago)33MITPHPPHP &gt;=8.0

Since Oct 28Pushed 1y agoCompare

[ Source](https://github.com/might-fail/php)[ Packagist](https://packagist.org/packages/might-fail/might-fail)[ RSS](/packages/might-fail-might-fail/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

Might Fail
==========

[](#might-fail)

A PHP library for handling errors without `try` and `catch` blocks.

Installation
------------

[](#installation)

```
composer require might-fail/might-fail
```

### Import

[](#import)

Import `mightFail` function into your source files.

```
use function MightFail\mightFail;
```

Usage
-----

[](#usage)

### Example 1

[](#example-1)

Requesting data from an API in a laravel application.

Here's the try-catch way to do it.

```
try {
    $response = Http::withToken($secretToken)->post('https://example.com')->throw();
    $json = $response->json();
    if ($json === null) {
        return response()->json(['error' => 'Invalid JSON.']);
    }

    return response()->json($json);
} catch (ConnectionException $e) {
    return response()->json(['error' => 'Connection failed.']);
} catch (TimeoutException $e) {
    return response()->json(['error' => 'Timeout.']);
} catch (HttpException $e) {
    return response()->json(['error' => 'HTTP error.']);
} catch (Exception $e) {
    return response()->json(['error' => 'Unknown error.']);
}
```

And, here's the `mightFail` way to do it.

```
use function MightFail\mightFail;

$either = mightFail(fn () => Http::withToken($secretToken)->post('https://example.com')->throw());

// You can also destructure the object like a tuple, if you want.
[$error, $response] = $either;

if ($either->error) {
    return match (get_class($either->error)) {
        ConnectionException::class => response()->json(['error' => 'Connection failed.']),
        TimeoutException::class => response()->json(['error' => 'Timeout.']),
        HttpException::class => response()->json(['error' => 'HTTP error.']),
        default => response()->json(['error' => 'Unknown error.']),
    };
}

$response = $either->result;

$either = mightFail(fn () => User::findOrFail(1));

if ($either->error !== null) {
    return response()->json([
        'error' => 'User not found.',
    ]);
}
```

### Example 2

[](#example-2)

Calling a repository method that might fail.

Here's the classic try-catch way to do it.

```
try {
    $shop = ShopRepository::visit($id);

    return response()->json([
        'success' => true,
        'data' => $shop,
    ]);
} catch (Exception $e) {
    return response()->json([
        'error' => 'Could not visit this shop',
    ]);
} finally {
    logger()->info('User tried to visit a shop', [
        'shop' => $shop,
        // ...
    ]);
}
```

And, here's the `mightFail` way to do it.

```
use function MightFail\mightFail;

[$error, $shop] = mightFail(fn () => ShopRepository::visit($id));

// Your finally block
logger()->info('User tried to visit a shop', [
    'shop' => $shop,
    // ...
]);

// Guard against error and handle it immediately
if ($error !== null) {
    return response()->json([
        'error' => 'Could not visit this shop',
    ]);
}

// Now we can safely return the shop
return response()->json([
    'success' => true,
    'data' => $shop,
]);
```

Might and Fail
--------------

[](#might-and-fail)

You can return `Might` or `Fail` classes from a method and natively return an `Either` type without `mightFail`function.

```
use MightFail\Either;
use MightFail\Fail;
use MightFail\Might;

public function visit(int $id): Either
{
    // ...

    if ($badThingHappened) {
        return Fail::from(new Exception('Something went wrong.'));
    }

    return Might::from($shop);
}
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

568d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/52e1ff42e229dea878734e452e55576c718cf0f03b003f4f953f8115180171c0?d=identicon)[iliaamiri](/maintainers/iliaamiri)

---

Top Contributors

[![iliaamiri](https://avatars.githubusercontent.com/u/37903573?v=4)](https://github.com/iliaamiri "iliaamiri (6 commits)")

---

Tags

error-as-valueserror-handlingmight-failtry-catch-finally

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/might-fail-might-fail/health.svg)

```
[![Health](https://phpackages.com/badges/might-fail-might-fail/health.svg)](https://phpackages.com/packages/might-fail-might-fail)
```

###  Alternatives

[symfony/stopwatch

Provides a way to profile code

2.8k387.2M918](/packages/symfony-stopwatch)[fruitcake/laravel-debugbar

PHP Debugbar integration for Laravel

19.1k662.9k29](/packages/fruitcake-laravel-debugbar)[jokkedk/webgrind

Webgrind is a Xdebug profiling web frontend in PHP5. It implements a subset of the features of kcachegrind and installs in seconds and works on all platforms. For quick'n'dirty optimizations it does the job.

3.3k193.0k](/packages/jokkedk-webgrind)[koriym/printo

An object graph visualizer.

1421.8M2](/packages/koriym-printo)[soloterm/dumps

A Laravel command to intercept dumps from your Laravel application.

125285.7k3](/packages/soloterm-dumps)[beyondcode/helo-laravel

HELO Laravel debug helper

90360.1k](/packages/beyondcode-helo-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
