Leveraging Typed Exceptions for Cleaner Error Handling
summary
Argues for replacing nulls and magic numbers with typed exceptions in PHP, showing concrete code: Doctrine's getSingleResult throws ORM exceptions that are caught and translated into a UserNotFoundException carrying the user ID, PHPUnit tests assert the exception, and controller/service layers shrink from nested checks to single-line calls. Demonstrates exception hierarchies, multi-catch (pipes), and sanitizing message input, claims this reduces lines of code, decouples signaling from handling, and makes APIs less confusing for consumers.
AI-written summary · about
chapters (22)
- 0:00Intro
- 0:54Validations: Painful
- 2:51Validations: Graceful
- 4:38Get Products for User
- 5:05Possible Stack
- 6:43getUserById(int Suserld): User
- 9:48Products Service: Alternative
- 12:16ProductsController: Option 1
- 12:45ProductsController: Option 2
- 13:44Impacts
- 15:34THE KEY IS THAT.
- 16:08UserNotFoundException
- 17:08Exceptions Flow
- 18:14UserRepository
- 18:27Simplifying Products Service
- 19:00Simplifying ProductsController
- 19:29Benefits of Exceptions
- 21:06Exceptions Best Practices
- 24:57ORM::getSingleResult
- 27:43Polymorphic Exceptions
- 30:07Catching Exceptions with "I"
- 30:52Exceptions & PHPUnit
related talks
51mLeveraging Typed Exceptions for Cleaner Error Handling
42mLeveraging Typed Exceptions for Cleaner Error Handling
35mHandling Exceptional Conditions with Grace and Elegance
32mJourney through "unhappy path" - Dealing with exceptional conditions
51mExceptions in PHP: more complicated than it appears
51mPHP ❤️ types
44mModern PHP Standards and Community
30mThe World of Laravel Exceptions
