core icon indicating copy to clipboard operation
core copied to clipboard

Fix exceptionToStatus in ApiResource and Operation

Open ArnoudThibaut opened this issue 3 years ago • 4 comments

Q A
Tickets #4848
License MIT

I open this PR to discuss about the possible solution to fix the exceptionToStatus feature inside ApiResource attribute and operation

Because of Symfony ErrorListener the request is duplicated without all the attributes needed to fetch the exception to status mapping.

A possible solution (the one I choose) could be to extend this listener to override the duplicateRequest method to also duplicate the attributes needed to get the exception to status mapping but I'm not realy satisfied with this solution so I'm really open to suggestions

ArnoudThibaut avatar Aug 02 '22 19:08 ArnoudThibaut

I don't see a better solution. Was it working in 2.6?

alanpoulain avatar Aug 02 '22 20:08 alanpoulain

Was it working in 2.6?

I did not find the feature in 2.6

ArnoudThibaut avatar Aug 03 '22 10:08 ArnoudThibaut

Right, it was introduced by this PR: https://github.com/api-platform/core/pull/3519. Could you target 2.7 then, since it's a bugfix?

alanpoulain avatar Aug 03 '22 12:08 alanpoulain

Could you target 2.7 then, since it's a bugfix?

It's done

ArnoudThibaut avatar Aug 03 '22 18:08 ArnoudThibaut

I think we have no other choice than extending the class+service via inheritance here. This could probably be revisited on 3.x, in conjunction with making the ErrorListener more cleanly extensible on Symfony's side.

This approach actually is the right way of doing it. The class gets registered as additional error listener, and basically adds some more data to the response for further processing. As long as we do not set the exception response, the follow-up listener (with a higher priority) will get triggered as well.

So, maybe we can add an integration test to make sure that the listener thereafter still gets triggered as well?

rvanlaak avatar Aug 22 '22 12:08 rvanlaak

What I meant is that, as a rule of thumb, decoration should be preferred over inheritance. Especially when it comes to third party code

chalasr avatar Aug 22 '22 17:08 chalasr

Ah @chalasr fair point, I did not see that it uses extension. My comment solely focussed on the service registration and related event listener priorities.

Is there a reason why the request needs to get duplicated? Is that because the _api_operation has to be available at a later moment? What about then just lowering the priority of the error listener to before the other listener that makes use of it? That could then also possibly allow removing the extend, as the duplication then isn't needed anymore.

When searching the repo for _api_operation this comment looks promising with relation to priorities ;-) https://github.com/api-platform/core/blob/6ab093e902a3a7b72f023458cd0f9e5f06eb4b50/src/Util/OperationRequestInitiatorTrait.php#L28

rvanlaak avatar Aug 22 '22 17:08 rvanlaak

Please check my last commit:

  • fixes a typo in the behat tests that used a wrong resource path (dummy_exceptions instead of dummy_exceptions_to_statuses)
  • uses service declaration

Let me know what you think of that.

soyuka avatar Aug 29 '22 15:08 soyuka

This is quite ready to be merged but we'll focus on the #4881 first.

soyuka avatar Aug 30 '22 15:08 soyuka

Thanks @ArnoudThibaut

soyuka avatar Sep 02 '22 08:09 soyuka

@soyuka This feature is not available in 3.0

dannyvw avatar Sep 19 '22 07:09 dannyvw

You're right, I'll check how to fix this today. It looks like https://github.com/api-platform/core/commit/2d705258978c8c5167356633503d379038d1b602 is on main though but I can't file the files, bad merge on our end?

soyuka avatar Sep 19 '22 08:09 soyuka

@soyuka Maybe this merge https://github.com/api-platform/core/pull/4910

dannyvw avatar Sep 19 '22 08:09 dannyvw

Looking to https://github.com/api-platform/core/commits/main/src/Symfony/EventListener, it seems that there isn't any commit on main that reverts this change?

rvanlaak avatar Sep 19 '22 12:09 rvanlaak

For reference, feature seems (re)introduced for branch 3.0 via PR https://github.com/api-platform/core/pull/4981

rvanlaak avatar Sep 20 '22 12:09 rvanlaak

This broke error handling in v2.7 + Symfony 4.4

The fix implemented by #3285 no longer works because the error_listener passed is now always present and never null.

https://github.com/api-platform/core/blob/6b483bc3a2696a908ea4cb68cda0344cfa224146/src/Symfony/Bundle/Resources/config/legacy/api.xml#L95

https://github.com/api-platform/core/blob/6b483bc3a2696a908ea4cb68cda0344cfa224146/src/Symfony/EventListener/ExceptionListener.php#L40

jackbentley avatar Nov 02 '23 19:11 jackbentley