oscarotero / psr7-middlewares

[DEPRECATED] Collection of PSR-7 middlewares

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AuraRouter curl stuck at 404 not found.

cwhsu1984 opened this issue · comments

I try to use AuraRouter with the middlewares, and it works fine if the routing exists. However, curl will not stop connection when the response is 404, 405, 406. I check the codes and find that there is no response in the code. I think we should at least add $response->getBody()->write("\n") or even better let us configure our own response message on 404, 405, 406.

oscarotero/psr7-middlewares/src/Middleware/AuraRouter.php

        if (!$route) {
            $failedRoute = $matcher->getFailedRoute();

            switch ($failedRoute->failedRule) {
                case 'Aura\Router\Rule\Allows':
                    return $response->withStatus(405); // 405 METHOD NOT ALLOWED

                case 'Aura\Router\Rule\Accepts':
                    return $response->withStatus(406); // 406 NOT ACCEPTABLE

                default:
                    return $response->withStatus(404); // 404 NOT FOUND
            }
        }

Hi
There's the error handler middleware to create error responses.

Say that I have a controller which processes input and write response to body with status code. When something goes wrong, I write 422 and the error message from the controller with code like

$response->getBody()->write('your input is invalid');
$response->withStatus(422)

With the error handler, I have to save those error related messages somewhere and try to regenerate these error messages from the error handler. This seems a bit abnormal for me because I cannot use the data that I write to the response in my controller. Do you have any suggestion to solve this?

Thank you.

Ok, the documentation of the statusCode option is missed, my fault.
You can configure which status codes you want to handle with the errorHandler:

$middleware[] = Middleware::errorHandler()
    ->statusCode(function ($code) {
        return $code >= 400 && $code < 600 && $code != 422;
    });

In this way, you can generate error responses in your controllers that are not overrided by the errorHandler.