• Stars
    star
    60
  • Rank 503,336 (Top 10 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Extend the Laravel exception handler to let service providers determine how custom exceptions should be handled.

Exception Handler

Author Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

SensioLabsInsight

This Laravel package lets you define the behavior of your application when a specific exception is thrown.

Laravel handles exceptions in app/Exceptions/Handler.php by default, but the more handlers you add the more this class gets cluttered and difficult to read and maintain.

Furthermore it is not possible for an external Laravel package to automatically register how its custom exceptions should be handled by the application where it has been installed.

This package lets you register custom exception handlers by using service providers, so that also external packages may register their own.

Please note: this package leverages the decorators design pattern, which allows you to keep using the Laravel default handler as you normally would. It just wraps the exception handler to extend its functionalities.

Install

Via Composer

composer require cerbero/exception-handler

If your Laravel version is prior to 5.5, please add this service provider in config/app.php

'providers' => [
    ...
    Cerbero\ExceptionHandler\Providers\ExceptionHandlerServiceProvider::class,
]

Usage

There are 3 types of handlers that can be registered:

  • Reporters, they log an exception or report it to an external service (e.g. Sentry, Bugsnag...)
  • Renderers, they render an exception into an HTTP response
  • Console Renderers, they render an exception to the console

The following examples show how to register each type of handler in the boot() method of a service provider:

use App\Exceptions\DebugException;
use App\Exceptions\ArtisanException;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Validation\ValidationException;

...

public function boot()
{
    // register a custom reporter to log all exceptions that are instances of - or extend - DebugException
    $this->app->make(ExceptionHandler::class)->reporter(function (DebugException $e) {
        $this->app['log']->debug($e->getMessage());
    });

    // register a custom renderer to redirect the user back and show validation errors
    $this->app->make(ExceptionHandler::class)->renderer(function (ValidationException $e, $request) {
        return back()->withInput()->withErrors($e->errors());
    });

    // register a custom console renderer to display errors to the console and stop the propagation of other exceptions
    $this->app->make(ExceptionHandler::class)->consoleRenderer(function (ArtisanException $e, $output) {
        $output->writeln('<error>' . $e->getMessage() . '</error>');
        return true;
    });
}

A handler is basically a callback that accepts the exception to handle as first parameter. You can register a global handler by omitting the exception type or type-hinting Exception.

Unlike reporters, renderers also accept a second parameter: an instance of Illuminate\Http\Request in case of a renderer or a Symfony\Component\Console\Output\OutputInterface in case of a console renderer.

It is also important to note that all registered handlers for an exception will be called until one of them returns a truthy value, in that case the exceptions propagation stops.

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

More Repositories

1

json-parser

🧩 Zero-dependencies lazy parser to read JSON of any dimension and from any source in a memory-efficient way.
PHP
676
star
2

lazy-json

🐼 Framework-agnostic package to load JSON of any dimension and from any source into Laravel lazy collections recursively.
PHP
235
star
3

enum

🎲 Zero-dependencies PHP library to supercharge enum functionalities.
PHP
188
star
4

command-validator

Validate Laravel console commands input.
PHP
160
star
5

laravel-enum

Discontinued. Enum generator for Laravel.
PHP
136
star
6

eloquent-inspector

πŸ•΅οΈ Inspect Laravel Eloquent models to collect properties, relationships and more.
PHP
115
star
7

Workflow

Laravel 5 package to create extendible and maintainable apps by harnessing the power of pipelines.
PHP
109
star
8

query-filters

Laravel package to filter Eloquent model records based on query parameters. Fully inspired by the Laracasts episode https://laracasts.com/series/eloquent-techniques/episodes/4
PHP
84
star
9

lazy-json-pages

πŸ“œ Framework-agnostic package to load items from any paginated JSON API into a Laravel lazy collection via async HTTP requests.
PHP
82
star
10

notifiable-exception

Laravel package to send notifications when some exceptions are thrown.
PHP
76
star
11

laravel-dto

Data Transfer Object (DTO) for Laravel
PHP
55
star
12

Auth

Laravel authentication module.
PHP
49
star
13

octane-testbench

β›½ Set of utilities to test Laravel applications powered by Octane.
PHP
42
star
14

sql-dumper

Laravel package to dump SQL queries, related EXPLAIN and location in code in different formats.
PHP
23
star
15

pest-plugin-laravel-octane

β›½ Pest plugin to test Laravel applications powered by Octane.
PHP
21
star
16

json-objects

Extract objects from large JSON files, endpoints or streams while saving memory.
PHP
20
star
17

dto

Data Transfer Object (DTO).
PHP
16
star
18

Transformer

Framework agnostic package to transform objects and arrays by manipulating, casting and mapping their properties.
PHP
14
star
19

Sublime-Text-PHP-and-Laravel-Snippets

Sublime Text snippets to ease development with PHP and Laravel.
13
star
20

console-tasker

🦾 Laravel package to create lean, powerful, idempotent and beautiful Artisan commands.
PHP
10
star
21

workflow-demo

Demo for Workflow repository
CSS
9
star
22

Date

Framework agnostic and easy to use tool to work with dates.
PHP
6
star
23

start

Mac service written in Automator to run several softwares and commands by pressing a hot key.
AppleScript
2
star
24

fluent-api

Framework agnostic starting point to perform fluent calls to any API.
PHP
1
star
25

Affiliate

PHP
1
star