craftcms / element-api

Create a JSON API/Feed for your elements in Craft.
MIT License
498 stars 56 forks source link

ElementApi returns 500 if site handle is not found #162

Closed akselikap closed 2 years ago

akselikap commented 2 years ago

Description

The title pretty much sums the issue. Before we were using element-api 2.8 and now we updated to 2.8.4. We have an API endpoint that is localized meaning that we use a query parameter to set the site criteria. One of the sites is now disabled. With the previous version we actually got the results from the endpoint even though the site was disabled. With the current version we get back HTTP 500 with body {"error":{"code":0,"message":"A server error occurred."}}. From the logs we can see that this is due to the fact that element-api can't find the site handle anymore due to the disabling of the site. We simultaneously upgraded version of Craft so I assume that the fact that the handle of the disabled site is not found anymore is due to that but I still think that a missing site handle should not result in HTTP status 500 and it should be 404 instead. We also had situations in the past where non-existing site handles were used with this API and that resulted in a 404. The caller of the API has no way of knowing whether the error is really an internal server error or if it just couldn't find the data. I see that this was updated in 2.8.1 and it's probably due to that.

Steps to reproduce

  1. Create an endpoint with criteria that contains a site with a non-existing/disabled handle.
  2. Make the request

Additional info

akselikap commented 2 years ago

Now that I think about this more a 404 or empty array is probably the correct result.

brandonkelly commented 2 years ago

If an exception occurs, it’s expected that a 500 response would be issued. It’s not Element API-spceific.

akselikap commented 2 years ago

But should an exception occur if it can't find something based on the given criteria? Shouldn't that be an empty response or a 404 instead of throwing an exception when the criteria itself is perfectly valid? In our use case we rely on the data provided by Element API and this makes error handling very hard because we can't distinguish between an actual server error or if there simply isn't any data for the given criteria.