Awesome Open Source
Awesome Open Source
Sponsorship

Friendly Exception


An exception interface that provides a friendly name and a possible solution. Error handlers may consider the interface to render additional information right at the error screen.

Latest Stable Version Total Downloads

Implementing friendly exception

To make exception friendly require this package and implement FriendlyExceptionInterface:

<?php

declare(strict_types=1);

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 practices:

  1. Make solution description as short as possible.
  2. Do not use HTML tags.
  3. A simple markdown is OK but its support is up to implementation.

Handling friendly exception

To make your exception handler render friendly exceptions:

use Yiisoft\FriendlyException\FriendlyExceptionInterface;

class ThrowableHandler
{
    public function handle(\Throwable $t)
    {
        if ($t instanceof FriendlyExceptionInterface) {
            // additional handling
        }
        // regular handling
    }
}

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
php (15,556
hacktoberfest (4,380
error-handling (90
exceptions (45
exception (16

Find Open Source By Browsing 7,000 Topics Across 59 Categories