Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow defining exception_to_status per operation #3519

Merged

Conversation

julienfalque
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tickets -
License MIT
Doc PR -

This PR allows introduces the exception_to_status attribute in operations and resources mapping.

The use case is when some domain exception should be transformed into a specific HTTP status on some API endpoints, but not on others.

For example: a method in some entity throws a NotPossible exception when called through an API endpoint. You want that endpoint to return a 400 Bad Request. But if the same entity method throws that exception when called from unrelated internal code, maybe that's actually an error in the code and should result in a 500 Internal Server Error.

@julienfalque julienfalque force-pushed the exception-to-status-per-operation branch 3 times, most recently from 0c89695 to 7d8b503 Compare April 24, 2020 08:22
@julienfalque julienfalque marked this pull request as ready for review April 24, 2020 08:35
@julienfalque julienfalque force-pushed the exception-to-status-per-operation branch from 7d8b503 to 1aec872 Compare November 1, 2020 14:04
Base automatically changed from master to main January 23, 2021 21:59
@alanpoulain alanpoulain force-pushed the exception-to-status-per-operation branch from 1aec872 to 2b5ab9b Compare March 4, 2021 15:37
@alanpoulain
Copy link
Member

Thank you for your PR!
Why not indeed.
Could you open a PR for the documentation too?

@julienfalque
Copy link
Contributor Author

Sure, see api-platform/docs#1309.

@julienfalque
Copy link
Contributor Author

@alanpoulain I updated ApiResource according to api-platform/docs#1309 (comment). As far as I understand, the root exception_to_status will be copied to $attributes so everything should work but I'm not actually sure. Do you mind having a look?

@julienfalque julienfalque force-pushed the exception-to-status-per-operation branch 2 times, most recently from ce4acdb to 7b83475 Compare March 5, 2021 19:00
@alanpoulain
Copy link
Member

Yes, it will be OK like this!

@julienfalque julienfalque force-pushed the exception-to-status-per-operation branch from 7b83475 to dd746c7 Compare March 5, 2021 20:34
@alanpoulain alanpoulain force-pushed the exception-to-status-per-operation branch from dd746c7 to 3e5ab24 Compare March 9, 2021 09:26
@alanpoulain alanpoulain merged commit 4717bd5 into api-platform:main Mar 9, 2021
@alanpoulain
Copy link
Member

Thank you @julienfalque.

@julienfalque
Copy link
Contributor Author

Thank you @alanpoulain.

@julienfalque julienfalque deleted the exception-to-status-per-operation branch March 9, 2021 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants