tobyzerner / json-api-server

A JSON:API server implementation in PHP.
https://tobyzerner.github.io/json-api-server/
MIT License
63 stars 21 forks source link

Bug: Exception::__construct(): Argument #2 ($code) must be of type int, array given #85

Closed bertramakers closed 7 months ago

bertramakers commented 9 months ago

When we (accidentally) try to update a resource with a different id in the URL as in the body, we get the following PHP error:

Exception::__construct(): Argument #2 ($code) must be of type int, array given

Thrown here: https://github.com/tobyzerner/json-api-server/blob/main/src/Endpoint/Concerns/SavesData.php#L43-L45

This line was changed in June I see: https://github.com/tobyzerner/json-api-server/commit/3bdac867aae76a5b94816c2f9a9ce03fb69828fb#diff-4e1c4cc32773a07b137197237630335d71c60511db9adc163b51a8c4c54f0443L43-R45

The issue seems to be that neither ConflictException nor its parent classes have a constructor to actually do something with the "source" array, and the native constructor of Exception gets called which expects an integer as second argument (for the exception code).

It does not occur for BadRequestException because that one does have a custom constructor: https://github.com/tobyzerner/json-api-server/commit/3bdac867aae76a5b94816c2f9a9ce03fb69828fb#diff-90e3070308e4f4b6b74f5bde2dc516df0f5eed0426e2777eea15f9bd867f7591

I wouldn't mind fixing this in a PR but I'm unsure how you'd want to proceed this. Either ConflictException should also support the "source" argument (but I'm not sure what impact that would have on the rest of the library) or no source should be included when throwing it.

tobyzerner commented 9 months ago

Thanks for the detailed report. I think I want to fix this as part of some broader improvements to error handling (i.e. every type of exception should have the ability to have a source, not just Bad Request)