PHPackages                             timefrontiers/php-has-errors - 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. timefrontiers/php-has-errors

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

timefrontiers/php-has-errors
============================

Standardized error handling trait for PHP classes

1.0.0(1mo ago)0214MITPHPPHP &gt;=8.1

Since Apr 15Pushed 1mo agoCompare

[ Source](https://github.com/timefrontiers/php-has-errors)[ Packagist](https://packagist.org/packages/timefrontiers/php-has-errors)[ RSS](/packages/timefrontiers-php-has-errors/feed)WikiDiscussions master Synced 1w ago

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

TimeFrontiers PHP Has Errors
============================

[](#timefrontiers-php-has-errors)

Standardized error handling trait for PHP classes with rank-based visibility filtering.

[![PHP Version](https://camo.githubusercontent.com/04744bae0a61d2ffe29c26f07a9612eae20445fc6feaeb77b3af1f0e9be6447c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e312d3838393242462e737667)](https://php.net/)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE)

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

[](#installation)

```
composer require timefrontiers/php-has-errors
```

Requirements
------------

[](#requirements)

- PHP 8.1+
- (Optional) `timefrontiers/php-instance-error` for advanced error extraction

Quick Start
-----------

[](#quick-start)

```
use TimeFrontiers\Helper\HasErrors;

class PaymentService {
  use HasErrors;

  public function charge(float $amount):bool {
    if ($amount _userError('charge', 'Amount must be positive');
      return false;
    }

    if (!$this->gateway->process($amount)) {
      $this->_systemError('charge', 'Gateway returned: ' . $this->gateway->lastError());
      $this->_userError('charge', 'Payment could not be processed. Please try again.');
      return false;
    }

    return true;
  }
}

// Usage
$payment = new PaymentService();
if (!$payment->charge(100.00)) {
  // Guest sees: "Payment could not be processed. Please try again."
  // Developer sees: "Gateway returned: Connection timeout" + user message
  $errors = (new InstanceError($payment))->get('charge');
}
```

Error Format
------------

[](#error-format)

Errors are stored as arrays with 5 elements:

```
[$min_rank, $code, $message, $file, $line]
```

IndexNameTypeDescription0`min_rank``int`Minimum AccessRank to view this error1`code``int`PHP error code (e.g., 256 = E\_USER\_ERROR)2`message``string`Human-readable error message3`file``string`Source file (auto-captured)4`line``int`Line number (auto-captured)AccessRank Enum
---------------

[](#accessrank-enum)

```
use TimeFrontiers\AccessRank;

AccessRank::GUEST;      // 0 - Public users
AccessRank::USER;       // 1 - Logged in users
AccessRank::ANALYST;    // 2
AccessRank::ADVERTISER; // 3
AccessRank::MODERATOR;  // 4 - Staff (can see internal errors)
AccessRank::EDITOR;     // 5
AccessRank::ADMIN;      // 6
AccessRank::DEVELOPER;  // 7 - Technical (can see system errors)
AccessRank::SUPERADMIN; // 8 - Can see debug errors
AccessRank::OWNER;      // 14 - Full access
```

Trait Methods
-------------

[](#trait-methods)

### Core Methods

[](#core-methods)

#### `_addError()`

[](#_adderror)

Add an error with full control:

```
$this->_addError(
  context: 'save',
  message: 'Database connection failed',
  min_rank: AccessRank::DEVELOPER->value,
  code: 256
);
// File and line are auto-captured from caller
```

#### `_mergeErrors()`

[](#_mergeerrors)

Merge errors from another instance:

```
if (!$this->repository->save($entity)) {
  $this->_mergeErrors($this->repository, 'save', 'create');
  return false;
}
```

### Helper Methods (Shortcuts)

[](#helper-methods-shortcuts)

MethodVisible ToUse For`_userError()`Everyone (rank 0+)Validation, user input, friendly messages`_internalError()`Staff (rank 4+)Business logic, process failures`_systemError()`Developers (rank 7+)SQL errors, connections, system issues`_debugError()`Superadmin (rank 8+)Sensitive debug info```
// User sees this
$this->_userError('login', 'Invalid email or password');

// Only developers see this
$this->_systemError('login', "SQL: {$sql}\nError: {$db_error}");
```

### Public Accessors

[](#public-accessors)

```
// Get all errors
$errors = $instance->getErrors();

// Check for errors
if ($instance->hasErrors('save')) { ... }
if ($instance->hasErrors()) { ... }  // Any context

// Count errors
$count = $instance->errorCount('save');
$total = $instance->errorCount();

// Clear errors
$instance->clearErrors('save');  // Specific context
$instance->clearErrors();        // All

// Get first error message
$message = $instance->firstError('save');
$message = $instance->firstError('save', AccessRank::GUEST->value);

// Get all messages
$messages = $instance->errorMessages('save');
$messages = $instance->errorMessages('save', AccessRank::GUEST->value);
```

Integration with InstanceError
------------------------------

[](#integration-with-instanceerror)

```
use TimeFrontiers\InstanceError;

$service = new MyService();
if (!$service->process()) {
  // Get errors filtered by current user's rank
  $errors = (new InstanceError($service))->get('process');

  // Get all errors (bypass rank filter)
  $all = (new InstanceError($service, true))->get('process');

  // Get only messages
  $messages = (new InstanceError($service))->get('process', true);
}
```

Complete Example
----------------

[](#complete-example)

```
use TimeFrontiers\Helper\HasErrors;
use TimeFrontiers\AccessRank;

class OrderService {
  use HasErrors;

  public function __construct(
    private OrderRepository $orders,
    private PaymentGateway $payment
  ) {}

  public function placeOrder(array $data):Order|false {
    // Validate
    if (empty($data['items'])) {
      $this->_userError('placeOrder', 'Cart is empty');
      return false;
    }

    // Create order
    $order = new Order($data);
    if (!$this->orders->save($order)) {
      $this->_mergeErrors($this->orders, 'save', 'placeOrder');
      $this->_userError('placeOrder', 'Unable to create order. Please try again.');
      return false;
    }

    // Process payment
    if (!$this->payment->charge($order->total)) {
      $this->_systemError('placeOrder', 'Payment failed: ' . $this->payment->lastError());
      $this->_userError('placeOrder', 'Payment declined. Please check your card details.');

      // Rollback
      $this->orders->delete($order);
      return false;
    }

    return $order;
  }
}

// Controller usage
$service = new OrderService($orders, $payment);

if ($order = $service->placeOrder($request->all())) {
  return Response::success('Order placed', $order);
}

// Get user-appropriate errors
$errors = (new InstanceError($service))->get('placeOrder', true);
return Response::error('Order failed', $errors);
```

Migration from Legacy Pattern
-----------------------------

[](#migration-from-legacy-pattern)

**Before:**

```
class MyClass {
  public $errors = [];

  public function doSomething():bool {
    if ($failed) {
      $this->errors['doSomething'][] = [0, 256, 'Failed', __FILE__, __LINE__];
      return false;
    }
    return true;
  }
}
```

**After:**

```
class MyClass {
  use HasErrors;

  public function doSomething():bool {
    if ($failed) {
      $this->_userError('doSomething', 'Failed');
      return false;
    }
    return true;
  }
}
```

License
-------

[](#license)

MIT License

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance89

Actively maintained with recent releases

Popularity9

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity42

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

55d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/dfcd9a5645cb2deb0771b9102be193d63b495564b1e06c0b85cb31bf028d0887?d=identicon)[ikechukwuokalia](/maintainers/ikechukwuokalia)

---

Top Contributors

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

---

Tags

phptraiterrorserror handling

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/timefrontiers-php-has-errors/health.svg)

```
[![Health](https://phpackages.com/badges/timefrontiers-php-has-errors/health.svg)](https://phpackages.com/packages/timefrontiers-php-has-errors)
```

###  Alternatives

[kkszymanowski/traitor

Add a trait use statement to existing PHP class

1305.5M16](/packages/kkszymanowski-traitor)[hedii/laravel-date-range

A date range trait with local scope methods for Laravel Eloquent models

2140.6k](/packages/hedii-laravel-date-range)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2518.6k](/packages/iteks-laravel-enum)

PHPackages © 2026

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