PHPackages                             amirhossein5/return-error - 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. amirhossein5/return-error

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

amirhossein5/return-error
=========================

v2.0.0(1y ago)02MITPHPPHP ^8.2CI passing

Since Dec 3Pushed 1y ago1 watchersCompare

[ Source](https://github.com/amirHossein5/return-error)[ Packagist](https://packagist.org/packages/amirhossein5/return-error)[ RSS](/packages/amirhossein5-return-error/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (5)Used By (0)

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

[](#installation)

- requires php8.2

```
composer require amirhossein5/return-error
```

Usage
-----

[](#usage)

Given you have a function that might return an error or value:

```
enum DivisionErrors {
    case DIVISION_BY_ZERO;
}

function divide(int $num, int $divideBy): int|ReturnError
{
    if ($divideBy === 0) {
        return new ReturnError(
            message: "can't divide by zero",
            type: DivisionErrors::DIVISION_BY_ZERO,
        );
    }

    return $num / $divideBy;
}

$divisionResult = divide(20, 0);
if ($divisionResult instanceof ReturnError) {
    if ($divisionResult->type === DivisionErrors::DIVISION_BY_ZERO) {
        // ...
    }

    // ...
}
```

Constructing ReturnError
------------------------

[](#constructing-returnerror)

```
new ReturnError();
```

With message:

```
new ReturnError(message: "something went wrong");
```

Or with a type which can be a *string* or *enum*:

```
new ReturnError(..., type: 'its_type');
new ReturnError(..., type: Enum::ENUM);
```

Reporting ReturnError
---------------------

[](#reporting-returnerror)

To log the error message in with stacktrace in laravel logs call `report()` method:

```
(new ReturnError())->report(); // local.ERROR:  {"exception":"[object] (Exception(code: 0):  at ...
(new ReturnError("with message"))->report(); // local.ERROR: message: with message {"exception...
(new ReturnError("with message", "its_type"))->report(); // local.ERROR: message: with message, type: its_type {"exception...

enum DivisionErrors: string { case DIVISION_BY_ZERO = 'division_by_zero'; }

(new ReturnError(type: DivisionErrors::DIVISION_BY_ZERO))->report(); // local.ERROR: type: DIVISION_BY_ZERO {"exception...
(new ReturnError(type: BackedDivideErrors::DIVISION_BY_ZERO))->report(); // local.ERROR: type: division_by_zero {"exception...

(new ReturnError())->report(additional: 'string'); // local.ERROR: additional: "string" {"exception...
(new ReturnError())->report(additional: ['given' => '...']); // local.ERROR: additional: {"given":"..."} {"exception...
```

Wrapping exceptions
-------------------

[](#wrapping-exceptions)

To wrap an exception into a ReturnError class instance use:

```
$result = ReturnError::wrap(function() {
    throw new Exception();
});
$result instanceof ReturnError; // true

$result = ReturnError::wrap(function(): int {
    return 2;
});
$result === 2; // true
```

Unwrapping ReturnError
----------------------

[](#unwrapping-returnerror)

In some cases you might want to throw the ReturnError if a function returns it:

```
$num = ReturnError::unwrap(divide(20, 0)); // throws exception
$num = ReturnError::unwrap(divide(20, 1)); // num is 20
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance45

Moderate activity, may be stable

Popularity2

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 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

Every ~44 days

Total

4

Last Release

398d ago

Major Versions

v1.0.0 → v2.0.02025-02-10

### Community

Maintainers

![](https://www.gravatar.com/avatar/8965321404f59b9bc2b2ec90f25cc2dd341030cb6863de0d4721a12b239ae079?d=identicon)[amirHossein5](/maintainers/amirHossein5)

---

Top Contributors

[![amirHossein5](https://avatars.githubusercontent.com/u/68776630?v=4)](https://github.com/amirHossein5 "amirHossein5 (21 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/amirhossein5-return-error/health.svg)

```
[![Health](https://phpackages.com/badges/amirhossein5-return-error/health.svg)](https://phpackages.com/packages/amirhossein5-return-error)
```

###  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)
