PHP code example of lcobucci / error-handling-middleware
1. Go to this page and download the library: Download lcobucci/error-handling-middleware library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lcobucci / error-handling-middleware example snippets
use Lcobucci\ContentNegotiation\ContentTypeMiddleware;
use Lcobucci\ErrorHandling\ErrorConversionMiddleware;
use Lcobucci\ErrorHandling\ErrorLoggingMiddleware;
// In a Laminas Mezzio application, it would look like this:
$application->pipe(ContentTypeMiddleware::fromRecommendedSettings( /* ... */ )); // Very first middleware
$application->pipe(new ErrorConversionMiddleware( /* ... */ ));
$application->pipe(new ErrorLoggingMiddleware( /* ... */ ));
// all other middleware.
declare(strict_types=1);
namespace My\Fancy\App\UserManagement;
use Lcobucci\ErrorHandling\Problem\ResourceNotFound;
use RuntimeException;
final class UserNotFound extends RuntimeException implements ResourceNotFound
{
}
declare(strict_types=1);
namespace My\Fancy\App\UserManagement;
use Lcobucci\ErrorHandling\Problem\Forbidden;
use Lcobucci\ErrorHandling\Problem\Typed;
use Lcobucci\ErrorHandling\Problem\Titled;
use Lcobucci\ErrorHandling\Problem\Detailed;
use RuntimeException;
use function sprintf;
final class InsufficientCredit extends RuntimeException implements Forbidden, Typed, Titled, Detailed
{
private $currentBalance;
public static function forPurchase(int $currentBalance, int $cost): self
{
$exception = new self(sprintf('Your current balance is %d, but that costs %d.', $currentBalance, $cost));
$exception->currentBalance = $currentBalance;
return $exception;
}
public function getTypeUri(): string
{
return 'https://example.com/probs/out-of-credit';
}
public function getTitle(): string
{
return 'You do not have enough credit.';
}
/** @inheritDoc */
public function getExtraDetails(): array
{
return ['balance' => $this->currentBalance]; // you might add "instance" and "accounts" too :)
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.