{"id":16418068,"url":"https://github.com/yceruto/ddd-error-handling","last_synced_at":"2026-05-18T05:47:29.368Z","repository":{"id":68432726,"uuid":"571110224","full_name":"yceruto/ddd-error-handling","owner":"yceruto","description":"DDD Error Handling","archived":false,"fork":false,"pushed_at":"2022-11-28T15:58:18.000Z","size":29,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-06T17:24:51.767Z","etag":null,"topics":["ddd","ddd-example","symfony"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yceruto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-27T07:30:36.000Z","updated_at":"2022-11-28T15:59:19.000Z","dependencies_parsed_at":"2023-07-02T18:45:47.038Z","dependency_job_id":null,"html_url":"https://github.com/yceruto/ddd-error-handling","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yceruto%2Fddd-error-handling","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yceruto%2Fddd-error-handling/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yceruto%2Fddd-error-handling/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yceruto%2Fddd-error-handling/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yceruto","download_url":"https://codeload.github.com/yceruto/ddd-error-handling/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240467572,"owners_count":19806004,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ddd","ddd-example","symfony"],"created_at":"2024-10-11T07:13:09.885Z","updated_at":"2025-10-28T12:33:54.421Z","avatar_url":"https://github.com/yceruto.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DDD Error Handling\n\n## Define your Domain exceptions\n\nIn Shared context, create generic Domain exceptions:\n\n```php\nnamespace App\\Shared\\Domain\\Exception;\n\nclass NotFound extends \\DomainException\n{\n}\n```\n\nNext, configure how the presentation layer will interpret the given exception in HTTP context and its status code:\n\n```yaml\n# config/packages/framework.yaml\nframework:\n    exceptions:\n        App\\Shared\\Domain\\Exception\\NotFound:\n            status_code: 404\n```\n\nAfter that, every custom Domain exception extending from `NotFound` will return a `404` status code:\n\n```php\nnamespace App\\Order\\Domain\\Model;\n\nuse App\\Shared\\Domain\\Exception\\NotFound;\n\nclass OrderNotFound extends NotFound\n{\n    public static function create(string $id): self\n    {\n        return new self(sprintf('The order \"%s\" could not be found.', $id));\n    }\n}\n```\n\n## Customizing the exception response\n\nInstalling the `Serializer` component will have a significant effect on how problem exceptions are handled. By default,\nthe `Symfony\\Component\\Serializer\\Normalizer\\ProblemNormalizer` will take care of it following the https://tools.ietf.org/html/rfc7807 spec.\n\nHowever, you can customize it by creating a new normalizer class:\n\n```php\nnamespace App\\Shared\\Presentation\\Serializer\\Normalizer;\n\nuse Symfony\\Component\\ErrorHandler\\Exception\\FlattenException;\nuse Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface;\n\nclass DomainExceptionNormalizer implements NormalizerInterface\n{\n    /**\n     * @param FlattenException $object\n     */\n    public function normalize(mixed $object, string $format = null, array $context = []): array\n    {\n        return [\n            'title' =\u003e $object-\u003egetStatusText(),\n            'status' =\u003e $object-\u003egetStatusCode(),\n            'detail' =\u003e $object-\u003egetMessage(),\n        ];\n    }\n\n    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool\n    {\n        return $data instanceof FlattenException;\n    }\n}\n```\n\nNote that here you don't care about the response format (JSON, XML) as it's another layer that instead will depend on\nthe request format (Content-Type, Accept).\n\n## Testing\n\nLook at the functional test to see how it works in practice:\n\n```ssh\n./bin/phpunit\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyceruto%2Fddd-error-handling","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyceruto%2Fddd-error-handling","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyceruto%2Fddd-error-handling/lists"}