Skip to content

Element API endpoints now always throw 500 instead of 404 error #157

@linusschwab

Description

@linusschwab

Description

After updating to the newest version of Element API, the endpoints now always throw a 500 instead of a 404 error if the element query returns no results.

I think this was introduced in version 2.8.1 according to the releasenotes: "Fixed a bug where most exceptions were resulting in 404 responses rather than 500s".

Is this change intentional like that and expected behaviour? If so, what needs to be changed for it to throw 404 errors again if an element is not found?
As a workaround, we temporary downgraded to version 2.7.0, where this works as expected.

Let me know if you need any additional information.

Steps to reproduce

  1. Create a simple test endpoint with a simple transformer function, for example:
'pages/<lang:{handle}>/<slug:{slug}>' => function ($lang, $slug) {
  return [
    'one' => true,
    'criteria' => [
      'section' => 'pages',
      'site' => $lang,
      'slug' => $slug
    ],
    'transformer' => function (craft\elements\Entry $entry) {
      return [
        'title' => $entry->title,
        'url' => $entry->url
     ];
    }
  ];
}
  1. Send a request with an invalid slug or site handle, where no element matches the criteria. Instead of a 404, a 500 error is returned. Expected would be a 404 and only a 500 error if something else goes wrong, for example in the transformer function.

Element API config

The defaults are set like that:

'defaults' => [
  'elementType' => Entry::class,
  'resourceKey' => 'elements',
  'pretty' => Craft::$app->getConfig()->general->devMode,   
  'paginate' => false,
  'cache' => 120
],

Additional info

  • Craft CMS version: 3.7.14
  • Element API version: 2.8.3
  • PHP version: 7.4.12
  • Database driver & version: MySQL 8.0.18
  • Plugins & versions:
    • Environment Label 3.2.0
    • Google Cloud Storage 1.4.1
    • Redactor 2.8.8
    • Super Table 2.6.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions