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
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.
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"
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
.
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.
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.
There are two ways to add solutions to your exceptions: on the exception itself, or via a solution provider.
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.
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:
// in your solution class
public function solutionProvidedByName(): string
{
return 'Flare';
}
public function solutionProvidedByLink(): string
{
return 'https://flareapp.io';
}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.