{"payload":{"allShortcutsEnabled":false,"fileTree":{"Resources/doc":{"items":[{"name":"examples","path":"Resources/doc/examples","contentType":"directory"},{"name":"1-setting_up_the_bundle.rst","path":"Resources/doc/1-setting_up_the_bundle.rst","contentType":"file"},{"name":"2-the-view-layer.rst","path":"Resources/doc/2-the-view-layer.rst","contentType":"file"},{"name":"3-listener-support.rst","path":"Resources/doc/3-listener-support.rst","contentType":"file"},{"name":"4-exception-controller-support.rst","path":"Resources/doc/4-exception-controller-support.rst","contentType":"file"},{"name":"annotations-reference.rst","path":"Resources/doc/annotations-reference.rst","contentType":"file"},{"name":"body_listener.rst","path":"Resources/doc/body_listener.rst","contentType":"file"},{"name":"conf.py","path":"Resources/doc/conf.py","contentType":"file"},{"name":"empty-content-status-code.rst","path":"Resources/doc/empty-content-status-code.rst","contentType":"file"},{"name":"format_listener.rst","path":"Resources/doc/format_listener.rst","contentType":"file"},{"name":"index.rst","path":"Resources/doc/index.rst","contentType":"file"},{"name":"param_fetcher_listener.rst","path":"Resources/doc/param_fetcher_listener.rst","contentType":"file"},{"name":"request_body_converter_listener.rst","path":"Resources/doc/request_body_converter_listener.rst","contentType":"file"},{"name":"requirements.txt","path":"Resources/doc/requirements.txt","contentType":"file"},{"name":"versioning.rst","path":"Resources/doc/versioning.rst","contentType":"file"},{"name":"view_response_listener.rst","path":"Resources/doc/view_response_listener.rst","contentType":"file"}],"totalCount":16},"Resources":{"items":[{"name":"config","path":"Resources/config","contentType":"directory"},{"name":"doc","path":"Resources/doc","contentType":"directory"}],"totalCount":2},"":{"items":[{"name":".github","path":".github","contentType":"directory"},{"name":"Context","path":"Context","contentType":"directory"},{"name":"Controller","path":"Controller","contentType":"directory"},{"name":"Decoder","path":"Decoder","contentType":"directory"},{"name":"DependencyInjection","path":"DependencyInjection","contentType":"directory"},{"name":"ErrorRenderer","path":"ErrorRenderer","contentType":"directory"},{"name":"EventListener","path":"EventListener","contentType":"directory"},{"name":"Exception","path":"Exception","contentType":"directory"},{"name":"Form","path":"Form","contentType":"directory"},{"name":"Negotiation","path":"Negotiation","contentType":"directory"},{"name":"Normalizer","path":"Normalizer","contentType":"directory"},{"name":"Request","path":"Request","contentType":"directory"},{"name":"Resources","path":"Resources","contentType":"directory"},{"name":"Response","path":"Response","contentType":"directory"},{"name":"Serializer","path":"Serializer","contentType":"directory"},{"name":"Tests","path":"Tests","contentType":"directory"},{"name":"Util","path":"Util","contentType":"directory"},{"name":"Validator","path":"Validator","contentType":"directory"},{"name":"Version","path":"Version","contentType":"directory"},{"name":"View","path":"View","contentType":"directory"},{"name":".gitattributes","path":".gitattributes","contentType":"file"},{"name":".gitignore","path":".gitignore","contentType":"file"},{"name":".php-cs-fixer.php","path":".php-cs-fixer.php","contentType":"file"},{"name":".readthedocs.yaml","path":".readthedocs.yaml","contentType":"file"},{"name":".styleci.yml","path":".styleci.yml","contentType":"file"},{"name":"CHANGELOG.md","path":"CHANGELOG.md","contentType":"file"},{"name":"FOSRestBundle.php","path":"FOSRestBundle.php","contentType":"file"},{"name":"LICENSE","path":"LICENSE","contentType":"file"},{"name":"README.md","path":"README.md","contentType":"file"},{"name":"UPGRADING-3.0.md","path":"UPGRADING-3.0.md","contentType":"file"},{"name":"composer.json","path":"composer.json","contentType":"file"},{"name":"phpunit","path":"phpunit","contentType":"file"},{"name":"phpunit.xml.dist","path":"phpunit.xml.dist","contentType":"file"}],"totalCount":33}},"fileTreeProcessingTime":7.415199,"foldersToFetch":[],"repo":{"id":1509011,"defaultBranch":"3.x","name":"FOSRestBundle","ownerLogin":"FriendsOfSymfony","currentUserCanPush":false,"isFork":false,"isEmpty":false,"createdAt":"2011-03-21T23:14:13.000Z","ownerAvatar":"https://avatars.githubusercontent.com/u/529709?v=4","public":true,"private":false,"isOrgOwned":true},"symbolsExpanded":false,"treeExpanded":true,"refInfo":{"name":"3.x","listCacheKey":"v0:1695814887.0","canEdit":false,"refType":"branch","currentOid":"e01be8113d4451adb3cbb29d7d2cc96bbc698179"},"path":"Resources/doc/4-exception-controller-support.rst","currentUser":null,"blob":{"rawLines":null,"stylingDirectives":null,"colorizedLines":null,"csv":null,"csvError":null,"dependabotInfo":{"showConfigurationBanner":false,"configFilePath":null,"networkDependabotPath":"/FriendsOfSymfony/FOSRestBundle/network/updates","dismissConfigurationNoticePath":"/settings/dismiss-notice/dependabot_configuration_notice","configurationNoticeDismissed":null},"displayName":"4-exception-controller-support.rst","displayUrl":"https://github.com/FriendsOfSymfony/FOSRestBundle/blob/3.x/Resources/doc/4-exception-controller-support.rst?raw=true","headerInfo":{"blobSize":"2.07 KB","deleteTooltip":"You must be signed in to make or propose changes","editTooltip":"You must be signed in to make or propose changes","ghDesktopPath":"https://desktop.github.com","isGitLfs":false,"onBranch":true,"shortPath":"34ea7ac","siteNavLoginPath":"/login?return_to=https%3A%2F%2Fgithub.com%2FFriendsOfSymfony%2FFOSRestBundle%2Fblob%2F3.x%2FResources%2Fdoc%2F4-exception-controller-support.rst","isCSV":false,"isRichtext":true,"toc":[{"level":1,"text":"Step 4: ExceptionController support","anchor":"step-4-exceptioncontroller-support","htmlText":"Step 4: ExceptionController support"}],"lineInfo":{"truncatedLoc":"58","truncatedSloc":"42"},"mode":"file"},"image":false,"isCodeownersFile":null,"isPlain":false,"isValidLegacyIssueTemplate":false,"issueTemplate":null,"discussionTemplate":null,"language":"reStructuredText","languageID":419,"large":false,"planSupportInfo":{"repoIsFork":null,"repoOwnedByCurrentUser":null,"requestFullPath":"/FriendsOfSymfony/FOSRestBundle/blob/3.x/Resources/doc/4-exception-controller-support.rst","showFreeOrgGatedFeatureMessage":null,"showPlanSupportBanner":null,"upgradeDataAttributes":null,"upgradePath":null},"publishBannersInfo":{"dismissActionNoticePath":"/settings/dismiss-notice/publish_action_from_dockerfile","releasePath":"/FriendsOfSymfony/FOSRestBundle/releases/new?marketplace=true","showPublishActionBanner":false},"rawBlobUrl":"https://github.com/FriendsOfSymfony/FOSRestBundle/raw/3.x/Resources/doc/4-exception-controller-support.rst","renderImageOrRaw":false,"richText":"

Step 4: ExceptionController support

\n

To map Exception classes to HTTP response status codes an exception map may\nbe configured, where the keys match a fully qualified class name and the values\nare either an integer HTTP response status code or a string matching a class\nconstant of the Symfony\\Component\\HttpFoundation\\Response class:

\n
fos_rest:\n    exception:\n        codes:\n            'Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException': 404\n            'Doctrine\\ORM\\OptimisticLockException': HTTP_CONFLICT\n        messages:\n            'Acme\\HelloBundle\\Exception\\MyExceptionWithASafeMessage': true
\n

If you want to display the message from the exception in the content of the\nresponse, add the exception to the messages map as well. If not only the status\ncode will be returned.

\n

If you know what status code you want to return you do not have to add a\nmapping, you can do this in your controller:

\n
<?php\n\nnamespace AppBundle\\Controller;\n\nuse Symfony\\Component\\HttpKernel\\Exception\\HttpException;\n\nclass UsersController extends Controller\n{\n    public function postUserCommentsAction($slug)\n    {\n        if (!$this->validate($slug)) {\n            throw new HttpException(400, \"New comment is not valid.\");\n        }\n    }\n}
\n

In order to make the serialization format of exceptions customizable it is possible to\nuse serializer normalizers.

\n

See how to create handlers for the JMS serializer and how to create normalizers for the Symfony serializer.

\n

That was it!

\n
\n

Note

\n

If you are receiving a 500 error where you would expect a different response, the issue\nis likely caused by an exception inside the ExceptionController (for example the serializer\nfailed). You should take a look at the logs of your app to see if an uncaught exception has been logged.

\n
\n\n
","renderedFileInfo":null,"shortPath":null,"symbolsEnabled":true,"tabSize":8,"topBannersInfo":{"overridingGlobalFundingFile":false,"globalPreferredFundingPath":null,"showInvalidCitationWarning":false,"citationHelpUrl":"https://docs.github.com/github/creating-cloning-and-archiving-repositories/creating-a-repository-on-github/about-citation-files","actionsOnboardingTip":null},"truncated":false,"viewable":true,"workflowRedirectUrl":null,"symbols":{"timed_out":false,"not_analyzed":true,"symbols":[]}},"copilotInfo":null,"copilotAccessAllowed":false,"csrf_tokens":{"/FriendsOfSymfony/FOSRestBundle/branches":{"post":"raUwN3pHnJgf9Ive97ioIo0xTExOtc-f3BXeoKRK5J7RA690xiPUT5yPk7XE26RwYNvApL1-6zbISowrHpec9g"},"/repos/preferences":{"post":"WEOXidQuoU3hkB5R_9tqIiPObjXCSi9ovq5i1jaXCbH8HjRg6LAx4-e2sXeL6GzB9dotHaYW0uEvAglh_bYv8A"}}},"title":"FOSRestBundle/Resources/doc/4-exception-controller-support.rst at 3.x ยท FriendsOfSymfony/FOSRestBundle"}