yiisoft/friendly-exception

An interface for friendlier exception

1.1.0 2021-10-26 21:43 UTC

README

68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667

Yii Friendly Exception


Latest Stable Version Total Downloads

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.

Requirements

  • PHP 7.1 or higher.

Installation

The package could be installed with composer:

composer require yiisoft/friendly-exception --prefer-dist

General usage

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. Use simple markdown.

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
    }
}

Do not forget to render markdown.

License

The Yii Friendly Exception is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack