|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Lcobucci\ErrorHandling; |
| 5 | + |
| 6 | +use Lcobucci\ContentNegotiation\UnformattedResponse; |
| 7 | +use Lcobucci\ErrorHandling\Problem\Detailed; |
| 8 | +use Lcobucci\ErrorHandling\Problem\Titled; |
| 9 | +use Lcobucci\ErrorHandling\Problem\Typed; |
| 10 | +use Psr\Http\Message\ResponseFactoryInterface; |
| 11 | +use Psr\Http\Message\ResponseInterface; |
| 12 | +use Psr\Http\Message\ServerRequestInterface; |
| 13 | +use Psr\Http\Server\MiddlewareInterface; |
| 14 | +use Psr\Http\Server\RequestHandlerInterface; |
| 15 | +use Throwable; |
| 16 | +use function array_key_exists; |
| 17 | + |
| 18 | +final class ErrorConversionMiddleware implements MiddlewareInterface |
| 19 | +{ |
| 20 | + private const CONTENT_TYPE_CONVERSION = [ |
| 21 | + 'application/json' => 'application/problem+json', |
| 22 | + 'application/xml' => 'application/problem+xml', |
| 23 | + ]; |
| 24 | + |
| 25 | + private const STATUS_URL = 'https://httpstatuses.com/'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ResponseFactoryInterface |
| 29 | + */ |
| 30 | + private $responseFactory; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var DebugInfoStrategy |
| 34 | + */ |
| 35 | + private $debugInfoStrategy; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var StatusCodeExtractionStrategy |
| 39 | + */ |
| 40 | + private $statusCodeExtractor; |
| 41 | + |
| 42 | + public function __construct( |
| 43 | + ResponseFactoryInterface $responseFactory, |
| 44 | + DebugInfoStrategy $debugInfoStrategy, |
| 45 | + StatusCodeExtractionStrategy $statusCodeExtractor |
| 46 | + ) { |
| 47 | + $this->responseFactory = $responseFactory; |
| 48 | + $this->debugInfoStrategy = $debugInfoStrategy; |
| 49 | + $this->statusCodeExtractor = $statusCodeExtractor; |
| 50 | + } |
| 51 | + |
| 52 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 53 | + { |
| 54 | + try { |
| 55 | + return $handler->handle($request); |
| 56 | + } catch (Throwable $error) { |
| 57 | + $response = $this->generateResponse($request, $error); |
| 58 | + |
| 59 | + return new UnformattedResponse( |
| 60 | + $response, |
| 61 | + $this->extractData($error, $response), |
| 62 | + ['error' => $error] |
| 63 | + ); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private function generateResponse(ServerRequestInterface $request, Throwable $error): ResponseInterface |
| 68 | + { |
| 69 | + $response = $this->responseFactory->createResponse($this->statusCodeExtractor->extractStatusCode($error)); |
| 70 | + |
| 71 | + $accept = $request->getHeaderLine('Accept'); |
| 72 | + |
| 73 | + if (! array_key_exists($accept, self::CONTENT_TYPE_CONVERSION)) { |
| 74 | + return $response; |
| 75 | + } |
| 76 | + |
| 77 | + return $response->withAddedHeader( |
| 78 | + 'Content-Type', |
| 79 | + self::CONTENT_TYPE_CONVERSION[$accept] . '; charset=' . $request->getHeaderLine('Accept-Charset') |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return array<string, mixed> |
| 85 | + */ |
| 86 | + private function extractData(Throwable $error, ResponseInterface $response): array |
| 87 | + { |
| 88 | + $data = [ |
| 89 | + 'type' => $error instanceof Typed ? $error->getTypeUri() : self::STATUS_URL . $response->getStatusCode(), |
| 90 | + 'title' => $error instanceof Titled ? $error->getTitle() : $response->getReasonPhrase(), |
| 91 | + 'details' => $error->getMessage(), |
| 92 | + ]; |
| 93 | + |
| 94 | + if ($error instanceof Detailed) { |
| 95 | + $data += $error->getExtraDetails(); |
| 96 | + } |
| 97 | + |
| 98 | + $debugInfo = $this->debugInfoStrategy->extractDebugInfo($error); |
| 99 | + |
| 100 | + if ($debugInfo !== null) { |
| 101 | + $data['_debug'] = $debugInfo; |
| 102 | + } |
| 103 | + |
| 104 | + return $data; |
| 105 | + } |
| 106 | +} |
0 commit comments