Playground Checkpoint
Design the exceptions for "slot taken", "shop closed" and "database down", throw them from the booking service and handle each one at the page — the expected output tells you when it's right.
The Challenge
Before launch, Marcus wants the booking service to stop returning magic strings. Today book() returns a booking number, or "taken", or "closed", or "error" — and the page prints "Booked!" for all of them. The database error is swallowed without a trace.
Open the starter in the PHP Playground with the button below and run it. Every request says "Booked!": "Booking #taken", "Booking #closed", "Booking #error". Turn each failure into a real exception, handle them in one place, and make the output match the expected output exactly.
What You Need to Do
- Run the starter and match each wrong line to a chapter of this Act.
- Create a marker interface
BookingExceptionand two domain exceptions that implement it:SlotTakenExceptionandShopClosedException, both extendingDomainException, each with a named constructor that writes the message (chapters 3 and 7). - Create
DatabaseUnavailableException, an infrastructure exception extendingRuntimeException(chapter 7). - Change
book()to return onlyintand throw instead of returning strings. Wrap thePDOExceptioninDatabaseUnavailableException, keeping it as the previous exception — never swallow it (chapters 2 and 3). - Make
handleBooking()the exception boundary: catch each exception, print the right message for the customer, and log the database failure with its cause throughlogError()(chapters 6 and 7). - Don't change the storage layer or the four requests at the bottom. The output must match the expected output exactly.
Stuck? A Few Hints
- throw ShopClosedException::on($day); replaces return "closed"; — the return type becomes int (chapter 2).
- Catch PDOException only around isTaken(), and throw new DatabaseUnavailableException("Could not check slot {$slot}", previous: $e) inside the catch (chapter 3).
- Keep the success echo inside the try, right after book(): when book() throws, it's skipped (chapter 2).
- $e->getPrevious() gives the PDOException for the log line; the customer never sees it (chapter 6).
Ready to Build It?
Opens the Playground, right in your browser — nothing to install.
A correct run prints five lines: Tom Hart's booking #4813 at Fri 6 Nov 11:00, "already taken" for Ann Lee, "closed on Sundays" for Sam Cole, then a LOG line naming DatabaseUnavailableException and its PDOException cause, and the friendly "something went wrong" message for Daniel Brooks. The starter prints "Booked!" four times, with "#taken", "#closed" and "#error" as booking numbers.
Before You Move On
If your output matches, BookBay's booking code fails the way good software does: loudly in the log, kindly on the screen, and in exactly the place designed for it. Next: files. Bookings are about to be saved to a JSON file — and two customers booking at the same moment will show why files need locks.
