core icon indicating copy to clipboard operation
core copied to clipboard

The exceptionToStatus mapping feature seems broken

Open rvanlaak opened this issue 3 years ago • 4 comments

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

rvanlaak avatar Jul 26 '22 16:07 rvanlaak

It looks like that the operation is null here https://github.com/api-platform/core/blob/main/src/Action/ExceptionAction.php#L61

dannyvw avatar Jul 31 '22 17:07 dannyvw

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

ArnoudThibaut avatar Aug 01 '22 12:08 ArnoudThibaut

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(),
    ]
)]

rvanlaak avatar Aug 01 '22 13:08 rvanlaak

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

sontungpham avatar Aug 02 '22 15:08 sontungpham