Skip to content

Commit 0b616a9

Browse files
DplusGDmitriy Goriaev
andauthored
Fix #59: Add examples of exception map in ExceptionResponder (#63)
Co-authored-by: Dmitriy Goriaev <[email protected]>
1 parent f8a3ffe commit 0b616a9

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/Middleware/ExceptionResponder.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,35 @@ final class ExceptionResponder implements MiddlewareInterface
2828
private Injector $injector;
2929

3030
/**
31+
* The `$exceptionMap` specified as an array can be in one of the following two formats:
32+
*
33+
* - callable format: `[\LogicException::class => callable, \DomainException::class => callable, ...]`
34+
* - int format: `[\Exception::class => 404, \DomainException::class => 500, ...]`
35+
*
36+
* When an exception is thrown, the map in callable format allows to take control of the response.
37+
* Сallable must return `Psr\Http\Message\ResponseInterface`. If specified exception classes are equal,
38+
* then the first one will be processed. Below are some examples:
39+
*
40+
* ```php
41+
* $exceptionMap = [
42+
* DomainException::class => function (\Psr\Http\Message\ResponseFactoryInterface $responseFactory) {
43+
* return $responseFactory->createResponse(\Yiisoft\Http\Status::CREATED);
44+
* },
45+
* MyHttpException::class => static fn (MyHttpException $exception) => new MyResponse($exception),
46+
* ]
47+
* ```
48+
*
49+
* When an exception is thrown, the map in int format allows to send the response with set http code.
50+
* If specified exception classes are equal, then the first one will be processed. Below are some examples:
51+
*
52+
* ```php
53+
* $exceptionMap = [
54+
* \DomainException::class => \Yiisoft\Http\Status::BAD_REQUEST,
55+
* \InvalidArgumentException::class => \Yiisoft\Http\Status::BAD_REQUEST,
56+
* MyNotFoundException::class => \Yiisoft\Http\Status::NOT_FOUND,
57+
* ]
58+
* ```
59+
*
3160
* @param callable[]|int[] $exceptionMap A must that should return a ResponseInterface or response status code.
3261
* @param ResponseFactoryInterface $responseFactory
3362
* @param Injector $injector

0 commit comments

Comments
 (0)