Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

returning JSON 404 #4

Open
weierophinney opened this issue Dec 31, 2019 · 1 comment
Open

returning JSON 404 #4

weierophinney opened this issue Dec 31, 2019 · 1 comment

Comments

@weierophinney
Copy link
Contributor

Since it is an API. When an endpoint does not exists (route is not found), shouldn't it return a 404 with response in JSON instead of error/404.phtml or some other HTTP code with a JSON response?


Originally posted by @rcapile at zfcampus/zf-apigility#191

@weierophinney
Copy link
Contributor Author

Apigility essentially maps routes to resources, which means that if routing does not match something, we don't know if that something should be an API resource, or a general web page; as such, returning JSON may be erroneous, if the page in question might map to an HTML response. We chose not to interfere with the normal MVC workflow and use the general tooling.

If you want to return a 404 with JSON, there are a couple of possibilities.

First, if the 404 might be due to an invalid identifier, you can either throw an exception with a 404 code, or return an ApiProblemResponse from your controller or REST resource.

Second, you can hook into the MvcEvent::EVENT_DISPATCH_ERROR, and check if the event error matches Application::ERROR_ROUTER_NO_MATCH; if it does, your listener could return an ApiProblemResponse with a 404 error code.

If this second functionality is something you feel would be a good general purpose addition to Apigility, we can consider adding a listener that users can optionally register with their Apigility applications for that purpose.


Originally posted by @weierophinney at zfcampus/zf-apigility#191 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant