PHPackages                             carlosupreme/error-or - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. carlosupreme/error-or

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

carlosupreme/error-or
=====================

A simple class to handle errors and results in a more elegant way.

1.0.0(1y ago)012MITPHPPHP ^8.3

Since Jun 19Pushed 1y ago1 watchersCompare

[ Source](https://github.com/carlosupreme/ErrorOr)[ Packagist](https://packagist.org/packages/carlosupreme/error-or)[ RSS](/packages/carlosupreme-error-or/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

ErrorOr PHP Library
===================

[](#erroror-php-library)

This PHP library provides a way to handle results that can either be a value or an error, similar to the `ErrorOr` pattern in other languages. This is particularly useful for methods that can fail, allowing you to handle errors and successful results in a unified way.

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

[](#installation)

You can install the library using Composer. Run the following command in your project's root directory:

```
composer require carlosupreme/error-or
```

Usage
-----

[](#usage)

### Error Class

[](#error-class)

The `Error` class represents different types of errors that can occur.

#### Creating an Error

[](#creating-an-error)

You can create errors using static methods on the `Error` class:

```
use Carlosupreme\ErrorOr\Error;

// Creating a failure error
$error = Error::failure();

// Creating an unexpected error
$error = Error::unexpected();

// Creating a validation error
$error = Error::validation('validation_code', 'Validation failed.');
```

#### Checking Error Details

[](#checking-error-details)

```
echo $error->getCode();         // Outputs the error code
echo $error->getDescription();  // Outputs the error description
echo $error->getType();         // Outputs the error type
```

### ErrorOr Class

[](#erroror-class)

The `ErrorOr` class is used to represent a result that can either be a value or an error.

#### Creating an ErrorOr

[](#creating-an-erroror)

You can create an `ErrorOr` instance from a value or from one or more errors:

```
use Carlosupreme\ErrorOr\ErrorOr;

// From a value
$result = ErrorOr::fromValue('some value');

// From a single error
$errorResult = ErrorOr::fromError($error);

// From multiple errors
$multiErrorResult = ErrorOr::fromErrors($error1, $error2);
```

#### Handling the Result

[](#handling-the-result)

You can check whether the result is an error and access the value or errors accordingly:

```
if ($result->isError()) {
    $errors = $result->getErrors();
    $firstError = $result->getFirstError();
} else {
    $value = $result->getValue();
}
```

Tests
-----

[](#tests)

To run the tests, use the following command:

```
vendor/bin/phpunit tests
```

Credits
-------

[](#credits)

This library is inspired by [amantinband's ErrorOr](https://github.com/amantinband/error-or). The original implementation in C# can be found [here](https://github.com/amantinband/error-or).

License
-------

[](#license)

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

This README provides an overview of the library, usage examples, instructions for running tests, and credits to the original author. Adjust the installation section and other parts as needed based on your actual project setup.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

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

Unknown

Total

1

Last Release

690d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/214a67e74df3577f6ac6b3f97a7bb147c07356406ac16c22cca05e6952034b47?d=identicon)[carlosupreme](/maintainers/carlosupreme)

---

Top Contributors

[![carlosupreme](https://avatars.githubusercontent.com/u/78663955?v=4)](https://github.com/carlosupreme "carlosupreme (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/carlosupreme-error-or/health.svg)

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

###  Alternatives

[ideea/language-detector

Detect languages by text

334.4k](/packages/ideea-language-detector)

PHPackages © 2026

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