PHPackages                             boohey/custom-try-catch - 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. boohey/custom-try-catch

ActiveLibrary

boohey/custom-try-catch
=======================

Laravel helper for API try-catch with logging and error ID

01—0%PHP

Since Mar 21Pushed yesterdayCompare

[ Source](https://github.com/Mugil1410/custom-try-catch)[ Packagist](https://packagist.org/packages/boohey/custom-try-catch)[ RSS](/packages/boohey-custom-try-catch/feed)WikiDiscussions main Synced today

READMEChangelog (1)DependenciesVersions (1)Used By (0)

Custom Try Catch (Laravel Package)
==================================

[](#custom-try-catch-laravel-package)

A simple and reusable Laravel helper to wrap your API or web logic with automatic try-catch, logging, and unique error IDs.

---

🚀 Installation
--------------

[](#-installation)

Install via Composer:

```
composer require boohey/custom-try-catch
```

---

⚙️ Usage
--------

[](#️-usage)

Wrap your controller or service logic using `apiTryCatch()`:

```
public function store()
{
    return apiTryCatch(function () {

        // Your business logic
        $data = [
            'name' => 'Test User'
        ];

        return response()->json([
            'success' => true,
            'data' => $data
        ]);

    });
}
```

---

❌ Example Error Handling
------------------------

[](#-example-error-handling)

If an exception occurs inside the callback:

```
return apiTryCatch(function () {
    throw new Exception("Something failed!");
});
```

---

📦 Sample API Response
---------------------

[](#-sample-api-response)

```
{
  "successCode": 0,
  "success": false,
  "message": "An error occurred",
  "errorId": "b3f8c6a2-8d3f-4c91-9c1e-2b9f1c5f6a21"
}
```

---

🌐 Web Response Behavior
-----------------------

[](#-web-response-behavior)

If the request is **not API (web request)**:

- Redirects back
- Shows error message with Error ID

Example:

```
Something went wrong. Error ID: b3f8c6a2-8d3f-4c91-9c1e-2b9f1c5f6a21

```

---

🧠 How It Works
--------------

[](#-how-it-works)

- Wraps your logic inside a try-catch block
- Generates a unique UUID for each error
- Logs error details using Laravel logger
- Returns clean JSON response for APIs
- Returns redirect response for web requests

---

🪵 Logging Example
-----------------

[](#-logging-example)

Errors are logged like:

```
API_ERROR: b3f8c6a2-8d3f-4c91-9c1e-2b9f1c5f6a21
Message: Something failed!
File: /app/Http/Controllers/UserController.php
Line: 25

```

---

✅ Features
----------

[](#-features)

- Simple helper function
- UUID-based error tracking
- Supports API &amp; Web responses
- Clean and consistent error format
- Uses Laravel native logging

---

🛠 Requirements
--------------

[](#-requirements)

- PHP &gt;= 8.0
- Laravel 8, 9, 10, or 11

---

📌 Best Practice
---------------

[](#-best-practice)

Use `apiTryCatch()` in:

- Controllers
- Service classes
- API endpoints

Avoid using it inside:

- Middleware (use global exception handler instead)

---

🔄 Future Improvements
---------------------

[](#-future-improvements)

- Configurable response format
- Middleware support
- Custom exception mapping
- Debug mode toggle

---

📄 License
---------

[](#-license)

MIT

###  Health Score

21

—

LowBetter than 21% of packages

Maintenance65

Regular maintenance activity

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity11

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.

### Community

Maintainers

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

---

Top Contributors

[![Mugil1410](https://avatars.githubusercontent.com/u/89967104?v=4)](https://github.com/Mugil1410 "Mugil1410 (3 commits)")

---

Tags

error-handlinglaravelphpphp-frameworkphp-librarytry-catch

### Embed Badge

![Health badge](/badges/boohey-custom-try-catch/health.svg)

```
[![Health](https://phpackages.com/badges/boohey-custom-try-catch/health.svg)](https://phpackages.com/packages/boohey-custom-try-catch)
```

PHPackages © 2026

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