The exceptionToStatus mapping feature seems broken
API Platform version(s) affected: 3.0.0-beta.2
Description
The exceptionToStatus mapping feature seems broken, when configuring error mapping on an ApiResource. The http response code still will be a http 500 error.
How to reproduce
namespace App\Application\ReadModel\Projection;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\Application\ReadModel\ProductProvider;
use App\Domain\ReadModel\Product\NotFoundException;
#[ApiResource(
provider: ProductProvider::class,
operations: [
new Get(
exceptionToStatus: [
[NotFoundException::class => 404]
],
)
]
)]
final class Product
{
// ...
}
Possible Solution
Not sure how I can debug this further, as we get the same http 500 error as on our other endpoint.
Additional Context
- Did do a fresh installation of API Platform v3.0.0-beta.2 on an existing project
- PHP 8.1
- Symfony 6.1
It looks like that the operation is null here https://github.com/api-platform/core/blob/main/src/Action/ExceptionAction.php#L61
I investigated this problem and I think that the exceptionToStatus option can't work on operations (and maybe also on Resources, I didn't try it) because of Symfony ErrorListener.
The request is duplicated without all the attributes needed to fetch the exception to status mapping
I've also tried the exceptionToStatus on the level of the resource, but it also returns a 500 where the mapped 404 is expected:
#[ApiResource(
provider: ProductProvider::class,
exceptionToStatus: [
[NotFoundException::class => 404],
],
operations: [
new Get(),
]
)]
I had the same problem. After installing a newer version v3.0.0-rc.1, it works again.
I write the config in config/packages/api_platform.yaml btw