Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
60 lines (47 sloc) 1.89 KB

Friendly Exception


The package provides an interface that could be implemented by an exception to provide a friendly name and a solution for fixing it. Error handlers may consider the interface to render additional information right at the error screen.

Latest Stable Version Total Downloads

Implementing friendly exception

In order to make exception friendly require this package and implement FriendlyExceptionInterface in your exception:

use Yiisoft\FriendlyException\FriendlyExceptionInterface;

class RequestTimeoutException extends \RuntimeException implements FriendlyExceptionInterface
{
    public function getName(): string
    {
        return "Request timed out";
    }
    
    public function getSolution(): ?string
    {
        return <<<'SOLUTION'
Likely it is a result of resource request is not responding in a timely fashion. Try increasing timeout.
SOLUTION;
    }
}

When returning solution consider the following best pracices:

  1. Make solution description as short as possible.
  2. Do not use HTML tags.

Handling friendly exception

In case you want your exception handler to render friendly exceptions you can do it like the following:

use Yiisoft\FriendlyException\FriendlyExceptionInterface;

class ThrowableHandler
{
    public function handle(\Throwable $t)
    {
        if ($t instanceof FriendlyExceptionInterface) {
            // additional handling
        }
        // regular handling
    }
}
You can’t perform that action at this time.