Skip to content

spatie/laravel-error-solutions

Repository files navigation

Display solutions on the Laravel error page

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package can display solutions on the Laravel error page. Here's how it looks:

INSERT IMAGE

For some solutions, the package will display a button that will automatically run the solution. Here's how that looks when you forget to run php artisan migrate:

INSERT IMAGE

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-error-solutions

You can publish the config file with:

php artisan vendor:publish --tag="laravel-error-solutions-config"

This is the contents of the published config file:

return [
    /**
     * Display solutions on the error page
     */
    'enabled' => true,

    /**
     * Enable or disable runnable solutions.
     *
     * Runnable solutions will only work in local development environments,
     * even if this flag is set to true.
     */
    'enable_runnable_solutions' => true,

    /**
     * This class is responsible for determining if a solution is runnable.
     *
     * In most cases, you can use the default implementation.
     */
    'runnable_solutions_guard' => Spatie\LaravelErrorSolutions\Support\RunnableSolutionsGuard::class,
];

Optionally, you can publish the views using

php artisan vendor:publish --tag="laravel-error-solutions-views"

Usage

After the package installed, you'll see solutions on the error page. If you want to disable this, you can set the enabled key in the config file to false.

Runnable solutions

Some solutions can be run automatically by clicking a button. This feature is only enabled in local development environments.

// INSERT IMAGE

The package uses the Spatie\LaravelErrorSolutions\Support\RunnableSolutionsGuard class to determine if the app is running locally. To have more control over the behaviour, you can extend this class and override its methods. You must set the runnable_solutions_guard key in the config file to your custom class.

Using AI to suggest solutions

The package can send your exceptions to Open AI that will attempt to automatically suggest a solution. In many cases, the suggested solutions is quite useful, but keep in mind that the solution may not be 100% correct for your context.

To generate AI powered solutions, you must first install this optional dependency.

composer require openai-php/client

To start sending your errors to OpenAI, you must set ERROR_SOLUTIONS_OPEN_AI_KEY in your .env file. The value should be your OpenAI key.

These bits of info will be sent to Open AI:

  • the error message
  • the error class
  • the stack frame
  • other small bits of info of context surrounding your error

It will not send the request payload or any environment variables to avoid sending sensitive data to OpenAI.

Creating your own solutions

There are two ways to add solutions to your exceptions: on the exception itself, or via a solution provider.

On the exception itself

The easiest way of adding a solution would to implement the ProvidesSolution interface on your exception.

Here's an example:

use Exception;
use Spatie\ErrorSolutions\Contracts\Solution;
use Spatie\ErrorSolutions\Contracts\ProvidesSolution;

class ExceptionWithSolution extends Exception implements ProvidesSolution
{
    public function __construct(string $message = '')
    {
        parent::__construct($message ?? 'My custom exception');
    }

    public function getSolution(): Solution
    {
        return new class implements Solution
        {
            public function getSolutionTitle(): string
            {
                return 'My custom solution';
            }

            public function getSolutionDescription(): string
            {
                return 'My custom solution description';
            }

            public function getDocumentationLinks(): array
            {
                return [
                    'Spatie docs' => 'https://spatie.be/docs',
                ];
            }
        };
    }
}

Optionally, you could add the solutionProvidedByName and solutionProvidedByLink method on the solution.

Using a solution provider

If you want to add solutions to exceptions that you can't modify, you can use a solution provider. A solution provider is a class that implements the ProvidesSolution interface. It will determine if it can provide a solution for a given exception.

Here's an example:

Adding "provided by" information to a solution

// in your solution class

public function solutionProvidedByName(): string
{
    return 'Flare';
}

public function solutionProvidedByLink(): string
{
    return 'https://flareapp.io';
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

About

Display solutions on the Laravel error page

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published