Provides a powerful error response system for Laravel

Overview

Laravel Exceptions

Laravel Exceptions was created by, and is maintained by Graham Campbell, and provides a powerful error response system for both development and production for Laravel. It optionally utilises the Whoops package for the development error pages. Feel free to check out the change log, releases, security policy, license, code of conduct, and contribution guidelines.

Banner

Build Status StyleCI Status Software License Packagist Downloads Latest Version

Installation

Laravel Exceptions requires PHP 7.2-8.1. This particular version supports Laravel 7-8.

Exceptions L5.1 L5.2 L5.3 L5.4 L5.5 L5.6 L5.7 L5.8 L6 L7 L8
9.4
10.1
11.3
12.1
13.1
14.1
15.1

To get the latest version, simply require the project using Composer:

$ composer require "graham-campbell/exceptions:^15.1"

Once installed, if you are not using automatic package discovery, then you need to register the GrahamCampbell\Exceptions\ExceptionsServiceProvider service provider in your config/app.php.

You then MUST change your App\Exceptions\Handler to extend GrahamCampbell\Exceptions\ExceptionHandler.

Whoops Support

If you want to have the debug error pages available, you're going to need to require Whoops:

$ composer require filp/whoops --dev

Our debug displayer will automatically detect the presence of Whoops. Feel free to go and have a read of our source code to give you a better understanding of how this works. Do note that we support only Whoops ^2.4.

Configuration

Laravel Exceptions supports optional configuration.

To get started, you'll need to publish all vendor assets:

$ php artisan vendor:publish

This will create a config/exceptions.php file in your app that you can modify to set your configuration. Also, make sure you check for changes to the original config file in this package between releases.

There are a few config options:

Exception Transformers

This option ('transformers') defines each of the exception transformers setup for your application. This allows you to turn your exceptions into other exceptions such as exceptions for perfect results when passed to the displayers. Note that this list is processed in order and subsequent transformers can still modify the results of previous ones if required.

Exception Displayers

This option ('displayers') defines each of the exception displayers setup for your application. These displayers are sorted by priority. Note that when we are in debug mode, we will select the first valid displayer from the list, and when we are not in debug mode, we'll filter out all verbose displayers, then select the first valid displayer from the new list.

Displayer Filters

This option ('filters') defines each of the filters for the displayers. This allows you to apply filters to your displayers in order to work out which displayer to use for each exception. This includes things like content type negotiation.

Default Displayer

This option ('default') defines the default displayer for your application. This displayer will be used if your filters have filtered out all the displayers, otherwise leaving us unable to displayer the exception.

Exception Levels

This option ('levels') defines the log levels for the each exception. If an exception passes an instance of test for each key, then the log level used is the value associated with each key.

Usage

There is currently no usage documentation for Laravel Exceptions, but we are open to pull requests.

Security

If you discover a security vulnerability within this package, please send an email to [email protected]. All security vulnerabilities will be promptly addressed. You may view our full security policy here.

License

Laravel Exceptions is licensed under The MIT License (MIT).

For Enterprise

Available as part of the Tidelift Subscription

The maintainers of graham-campbell/exceptions and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

Comments
  • I found an issue that this doesn't able to catch FatalException

    I found an issue that this doesn't able to catch FatalException

    When under production mode, if there is an error occurs in like say config/app.php, anything, just type an "ServiceProvider" that doesn't exists to test the error:

    • it result that this page only display blank page with 500 error, and when i try catch parent::render($request, $e) the error is that "Class view does not exist"

    However, for errors that occur in Controllers, Middlewares, are able to produce 500 Internal Server Error that this package provided.

    Hope you know what I am mentioning about....

    opened by libern 36
  • Whoops page when AuthenticationException is thrown in laravel 5.3

    Whoops page when AuthenticationException is thrown in laravel 5.3

    There is an issue with new laravel 5.3 apps.

    The new auth middleware Illuminate\Auth\Middleware\Authenticate throws an AuthenticationException when the user is not logged in. The default exception handler handles this by calling the unauthenticated method in the App\Exceptions\Handler.

    https://github.com/laravel/framework/blob/5.3/src/Illuminate/Foundation/Exceptions/Handler.php#L135

    Currently the GrahamCampbell\Exceptions\NewExceptionHandler does not handle this Exception in any particular way and ends up displaying the whoops page.

    I made an example repo for reference: https://github.com/scrubmx/issue-example/commits/master

    opened by scrubmx 34
  • Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable.

    Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable.

    I was getting a "Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable" error while running phpUnit under Laravel 5.2 and managed to replicate it on a new project with just Laravel-Exceptions. I tracked it down to possible a missing comment in the installation documentation. It seems that, atleast on 5.2, you need to change the following in bootstrap\app.php

    $app->singleton( Illuminate\Foundation\Exceptions\Handler::class, App\Exceptions\Handler::class );

    to

    $app->singleton( GrahamCampbell\Exceptions\ExceptionHandler::class, App\Exceptions\Handler::class );

    Does that seem right? And if so, could you please add it to the installation description to help others?

    Funny enough the site worked fine, just the phpUnit that was complaining about it.

    opened by tkprocat 22
  • All non `HttpExceptionInterface` exceptions transformed to 500's?

    All non `HttpExceptionInterface` exceptions transformed to 500's?

    So I'm finding that this package is causing a bit of grief with the exceptions.

    Let's say I make an API request to get a resource, but my app denies the request through laravels ACL, this throws a AuthorizationException which does not implement the HttpExceptionInterface Interface.

    Laravel doesn't add a code to this exception(it really should be a 401 or 403) so I wrote a transformer that returns the exception but with a 401 code.

    The issue is that because it doesn't implement HttpExceptionInterface when this package uses Symfony's FlattenException the status code gets reset to 500 which is annoying, but manageable.

    The real issue then comes in when we hit ExceptionInfo::generate because again we don't Implement HttpExceptionInterface we end up with the generic information from the json file.

    So by the time the user get's the error they see selection_040

    So basic question, why are we so reliant on HttpExceptionInterface?

    opened by hailwood 17
  • Duplicate Content-Type Headers Causing Apache Error?

    Duplicate Content-Type Headers Causing Apache Error?

    Installed Laravel-Exceptions with Whoops on a project. Threw an exception to test and was surprised to be greeted by a generic server-generated 500 error page instead of whoops. My apache logs (using MAMP) indicated that duplicate 'Content-Type' were causing an error. I inspected the response return by the GrahamCampbell\Exceptions\ExceptionHandler::render() method and noticed that it specified Content-Type: text/html where as Illuminate's handler did not.

    I'm not sure where the content type is being inserted after the Handler does its thing, but this duplicate is clearly causing an issue. I was able to remedy the situation by doing the following, but a more permanent fix might be better.

    public function render($request, Exception $e)
    {
        if ($e instanceof ModelNotFoundException) {
            $e = new NotFoundHttpException($e->getMessage(), $e);
        }
        $response = parent::render($request, $e);
        $response->headers->remove('Content-Type');
        return $response;
    }
    
    opened by frasmage 16
  • Error pages (ie. 404) are not being shown; converts to a 500 page

    Error pages (ie. 404) are not being shown; converts to a 500 page

    After installing this package, our 404, 500, 503, etc pages are gone. All we get is a white page.

    In bootstrap/app.php this is how I call your exceptionhandler

    $app->singleton(
        'Illuminate\Contracts\Debug\ExceptionHandler',
        'GrahamCampbell\Exceptions\ExceptionHandler' // before: App\Exceptions\Handler.
    );
    

    If I change the ExceptionHandler to use the native App\Exceptions\Handler error pages are shown just fine. Therefore it must be something to do with your package.

    If debugmode is true I see default Whoops page. If debug mode is false I see white pages.

    opened by simplenotezy 15
  • Use with bugsnag

    Use with bugsnag

    I am using bugsnag for my applications which means my handler.php is extending "Bugsnag\BugsnagLaravel\BugsnagExceptionHandler".

    How can I use this package with bugsnag?

    opened by simplenotezy 14
  • Generate Unique Error Codes

    Generate Unique Error Codes

    We should generate unique error codes and attach them to the logs, and present them to the user. That way if a user wants to contact support, they can provide us with the unique error code.

    opened by GrahamCampbell 14
  • AuthenticationException is rendered on routes with middleware auth instead of redirect to login page

    AuthenticationException is rendered on routes with middleware auth instead of redirect to login page

    Laravel 5.4 & Whoops

    When using Laravel's Illuminate\Foundation\Exceptions\Handler and visiting a protected route with auth middleware, the browser is redirected to a login page. (expected behaviour)

    When using GrahamCampbell\Exceptions\NewExceptionHandler an AuthenticationException is rendered and not redirected to login page. (unexpected behaviour)

    Looking at Laravel handler there is a check for AuthenticationException and the function unauthenticated is called

    I couldn't find something similar on ExceptionHandlerTrait

    The solution is to change the render function on App\Exceptions\Handler to take this into account

        public function render($request, Exception $exception)
        {
    		if ($exception instanceof \Illuminate\Auth\AuthenticationException){
    			return $this->unauthenticated($request, $exception);
    		}
                    return parent::render($request, $exception);
        }
    
    opened by chepe263 12
  • Proposal Custom Displayers

    Proposal Custom Displayers

    I would really like to see custom displayers and the ability to customize the existing ones. My idea is to create a configuration file with all displayers that should be loaded as well as a default one. The configuration file would look something like to this:

    /*
    |--------------------------------------------------------------------------
    | Default Exception Displayer
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the exception displayers below you wish to
    | use as your default displayer for all exceptions.
    |
    */
    
    'default' => 'plain',
    
    /*
    |--------------------------------------------------------------------------
    | Exception Displayers
    |--------------------------------------------------------------------------
    |
    | Here are each of the exception displayers setup for your application.
    | Default displayers has been included, but you may add as many displayers
    | as you would like.
    |
    */
    
    'displayers' => [
        'array' => 'GrahamCampbell\Exceptions\Displayers\ArrayDisplayer',
        'debug' => 'GrahamCampbell\Exceptions\Displayers\DebugDisplayer',
        'plain' => 'GrahamCampbell\Exceptions\Displayers\PlainDisplayer',
    ],
    

    Each displayer class should have a class that we can call to determine if this displayer should be used or not. Currently named it use, there probably is a much better name.

    /**
     * This is the array displayer class.
     *
     * @author Graham Campbell <[email protected]>
     */
    class ArrayDisplayer implements DisplayerInterface
    {
        /**
         * Get the content associated with the given exception.
         *
         * @param \Exception $exception
         * @param int        $code
         *
         * @return array
         */
        public function display(Exception $exception, $code)
        {
            $info = ExceptionInfo::generate($code, $exception->getMessage());
    
            return ['success' => false, 'code' => $info['code'], 'msg' => $info['extra']];
        }
    
        /**
         * The method to decide if this displayer should be used or not.
         *
         * @param \Exception               $exception
         * @param \Illuminate\Http\Request $request
         *
         * @return bool
         */
        public function use(Exception $exception, Request $request)
        {
            return $request->ajax();
        }
    }
    

    Then at last the getContent method in the ExceptionHandler class loops over the displayers specified in the configuration file and then sees which one to use. In this way we won't break any current setup. You wont have to publish the configuration if you don't want to.

    What do you think @GrahamCampbell? Is this solution too complex?

    opened by vinkla 11
  • Not able to handle 500 error through handler

    Not able to handle 500 error through handler

    I have installed Laravel 8 with GrahamCampbell/Exception 14.1. When I get 500 error then it just show plain text Internal Server Error and if I check error log then there is error local.ERROR: ReflectionException: Class GrahamCampbell\Exceptions\Transformers\AuthTransformer does not exist.

    Code is:

    public function render($request, Throwable $e) { return parent::render($request, $e);

    }

    Please help me to fix what exactly causing this

    opened by miamarconi 10
  • Target [GrahamCampbell\Exceptions\Identifier\IdentifierInterface] is not instantiable.

    Target [GrahamCampbell\Exceptions\Identifier\IdentifierInterface] is not instantiable.

    PHP version: (hint: php --version)

    PHP 7.4.24 (cli) (built: Sep 23 2021 21:24:59) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
        with Zend OPcache v7.4.24, Copyright (c), by Zend Technologies
    

    Description

    I was using "graham-campbell/exceptions": "^15.1", and it is registered with the service provider under config/app.php -> providers using GrahamCampbell\Exceptions\ExceptionsServiceProvider::class,. However I still get the error:

    In Container.php line 1017:
    
      Target [GrahamCampbell\Exceptions\Identifier\IdentifierInterface] is not instantiable.
    
    

    I tried to add:

        "extra": {
            "laravel": {
                "providers": [
                    "GrahamCampbell\\Exceptions\\ExceptionsServiceProvider"
                ]
            }
        },
    

    still it throws same error. The laravel version I used is ^7.0.

    Do you know why it happens?

    How to reproduce

    If I use laravel ^6.0 with "graham-campbell/exceptions": "^12.1". Everything is fine with the code.

    Possible Solution

    Additional context

    opened by kcan 13
Releases(v16.0.0)
Owner
Graham Campbell
OSS Maintainer | Laravel | StyleCI
Graham Campbell
Ignition: a beautiful error page for Laravel apps

Ignition is a beautiful and customizable error page for Laravel applications running on Laravel 5.5 and newer. It is the default error page for all Laravel 6 applications.

Spatie 260 Dec 29, 2022
Your users do not always report errors, LaraBug does. LaraBug is a simple to use and implement error tracker built for the Laravel framework.

Your users do not always report errors, LaraBug does. LaraBug is a simple to use and implement error tracker built for the Laravel framework. This rep

LaraBug 197 Dec 9, 2022
Easily capture every incoming request and the corresponding outgoing response in your Laravel app.

Easily capture every incoming request and the corresponding outgoing response in your Laravel app. This package is designed to work only with the Lara

Mark Townsend 22 Nov 15, 2022
This Package helps you in laravel application to log all desired activity for each request from request entry point to generate response at a single snapshot.

Laravel Scenario Logger This Package helps you in laravel application to log all desired activity for each request from request entry point to generat

Mehrdad Mahdian 6 Sep 27, 2021
Convert remote api response data into laravel model

laravel remote model Create remote driver to convert remote api request into laravel model. 中文文档 日本語文書 overview Install the version between laravel5.5

张子彬 15 Aug 11, 2022
A Laravel response helper methods.

A Laravel response helper methods. The package respond provides a fluent syntax to form array or json responses.

Najm Njeim 5 Nov 2, 2021
Simple and ready to use API response wrapper for Laravel.

Laravel API Response Simple Laravel API response wrapper. Installation Install the package through composer: $ composer require obiefy/api-response Re

Obay Hamed 155 Dec 14, 2022
Laravel Custom Response Messages from Passport Oauth2 Server

This is a sample repository showing how to install Oauth2 server using Laravel's passport package and customize its responses.

M. Ismail 7 Nov 22, 2022
Laravel Response Formatter

I created this package to make it easier to format the response from a controller. I have used this package in my projects and I hope you enjoy it!

aris wahyudiyanto 12 Dec 5, 2022
A lightweight package for handling API error responses.

Laravel API Errors This package provides an easy way to manage and handle error response for JSON API's. Installation You can install the package via

3 SIDED CUBE 2 Feb 9, 2022
Laravel is accessible, powerful, and provides tools required for large, robust applications.

Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be trul

Web & Mobile | eCommerce | Full-Stack Developer 4 Nov 24, 2022
A Magento Incident Response Plan Template

A Magento centric Incident Response Plan Template Introduction This will provide you with our defined process and procedures to use when responding to

Talesh Seeparsan 75 Dec 30, 2022
Simple PSR-7 compatible response sender

Simple PSR-7 compatible response sender

Lazzard 4 Nov 5, 2022
Simple package to handle response properly in your API.

Simple package to handle response properly in your API. This package uses Fractal and is based on Build APIs You Won't Hate book.

Optania 375 Oct 28, 2022
🔔 Flasher is a powerful and flexible flash notification system for PHP, Laravel, Symfony

A powerful and flexible flash notifications system for PHP, Laravel, Symfony ?? PHP Flasher helps you to add flash notifications to your PHP projects.

PHP Flasher 158 Jan 4, 2023
Extend Kirby’s templates with a powerful layout system

Kirby Layouts plugin This plugin extends Kirby’s templates with a powerful layout system. Installation Download Download and copy this repository to /

Kirby 3 39 Dec 28, 2022
cybercog 996 Dec 28, 2022
Powerful Unsplash package for Laravel

Powerful Unsplash package for Laravel Provides a fluent api to use the Unsplash within Larvel applications. Use public actions or store images directl

null 54 Dec 25, 2022
Powerful REPL for the Laravel framework.

Introduction Laravel Tinker is a powerful REPL for the Laravel framework. Official Documentation Documentation for Tinker can be found on the Laravel

The Laravel Framework 7.1k Jan 3, 2023