thephpleague / route

Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.
http://route.thephpleague.com
MIT License
651 stars 126 forks source link

Catch Exception Best Way #302

Closed ursoforte closed 3 years ago

ursoforte commented 3 years ago

Hi,

I do not found in the Docs an example to catch the NotFoundException and MethodNotAllowedException exception, simple. Without creating new custom strategies. The idea is to use the default ApplicationStrategy.

Someone can add an example code or write an external reference.

How I do with simple route:

The case below is perfect for a simple URL.

$router->get('/simple[/]', function (ServerRequestInterface $request): ResponseInterface {
    return new RedirectResponse('/notfound');
});

But, when I use custom params like this, and the UUID code is wrong, so I want to redirect the request to NotFound.

$router->get('/simple/{param1:uuid}', Controller\Simple::class);

Note that I have many HTTP Routes like this, so I don't want to use wildcard routes at all times.

What is the best way to catch exceptions in ApplicationStrategy. Thanks.

philipobenito commented 3 years ago

Application strategy is just a base and should be extended. Otherwise you’d need to wrap the dispatch call in a try catch

Sent from ProtonMail for iOS

On Mon, Jun 7, 2021 at 21:32, Bernardo Castro @.***> wrote:

Hi,

I do not found in the Docs an example to catch the NotFoundException and MethodNotAllowedException exception, simple. Without creating new custom strategies. The idea is to use the default ApplicationStrategy.

Someone can add an example code or write an external reference.

How I do with simple route:

The case below is perfect for a simple URL.

$router->get('/simple[/]', function (ServerRequestInterface $request): ResponseInterface { return new RedirectResponse('/notfound'); });

But, when I use custom params like this, and the UUID code is wrong, so I want to redirect the request to NotFound.

$router->get('/simple/{param1:uuid}', Controller\Simple::class);

Note that I have many HTTP Routes like this, so I don't want to use wildcard routes at all times.

What is the best way to catch exceptions in ApplicationStrategy. Thanks.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

ursoforte commented 3 years ago

Hi, thank you. Using the try-catch in dispatch is the way that I don't want to do it. I think something like using the hot way:

$strategy->setNotFoundException(new MyNotFoundException);
$strategy->setNotAllowedException(new MyNotAllowedException);
philipobenito commented 3 years ago

Yeah, I was saying the try catch is your only option if you don’t create a custom strategy. Custom strategy extending application strategy is absolutely the way to go.

Sent from ProtonMail for iOS

On Tue, Jun 8, 2021 at 00:20, Bernardo Castro @.***> wrote:

Hi, thank you. Using the try-catch in dispatch is the way that I don't want to do it. I think something like using the hot way:

$strategy->setNotFoundException(new MyNotFoundException); $strategy->setNotAllowedException(new MyNotAllowedException);

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.