Leveraging Typed Exceptions for Cleaner Error Handling
summary
Demonstrates that replacing nulls and magic return codes with typed exceptions yields flatter, clearer code: catch Doctrine ORM no-result and non-unique-result cases, throw a UserNotFoundException, simplify service and controller layers, and verify behavior with PHPUnit expectException. Uses getSingleResult, custom exception classes and small refactors; tests move from failing on raw ORM exceptions to passing once the custom exception is thrown, removing ad hoc -1/null signaling and reducing cascading error checks.
AI-written summary · about
chapters (10)
- 0:00Introduction
- 1:03The problem with validation
- 3:44Introducing exceptions
- 5:46Complex error handling case
- 14:22Impact of manual error codes
- 20:36Exceptions as a solution
- 24:23Exception best practices
- 28:02Polymorphic exception handling
- 33:21Testing and implementation
- 40:31Conclusion and Q&A
related talks
38mLeveraging Typed Exceptions for Cleaner Error Handling
51mLeveraging Typed Exceptions for Cleaner Error Handling
35mHandling Exceptional Conditions with Grace and Elegance
31mException design - the art of making debugging more accurate
30mThe World of Laravel Exceptions
59mDoctrine ORM Good Practices and Tricks
51mPHP ❤️ types
51mExceptions in PHP: more complicated than it appears
