Odette's nephew is worried. The front controller catches every exception: the day the gazette has a database and that database breaks down, readers will see “Lost in the post” instead of a real error, and nobody will go looking for the problem. We need a separate exception that means “this article does not exist”, and we should catch only that one.
Your mission
- In
src/Contenu/ArticleIntrouvable.php, declare the classArticleIntrouvable(ArticleNotFound), which inherits from PHP'sExceptionclass:class ArticleIntrouvable extends Exception, withuse Exception;below thenamespace. - Its constructor receives the slug (
public readonly string $slug) and passes the messageNo article has the slug “{$slug}”., with the slug it received, toparent::__construct(). - In
Redaction::article(), throw it instead of theException:throw new ArticleIntrouvable($slug);. - In the front controller,
catch (Exception)becomescatch (ArticleIntrouvable). Import it at the top of the file withuse Gazette\Contenu\ArticleIntrouvable;: acatchon a class PHP doesn't know catches nothing, without any warning.
Reminder
extends creates a child class: ArticleIntrouvable gets everything Exception has (the message, getMessage(), the line where it was thrown…), and adds its slug to it. An ArticleIntrouvable is an Exception: a catch (Exception) would catch it too. But catch (ArticleIntrouvable) catches only that one, and lets the others through.
The child's constructor replaces the parent's. So that the message is still stored, the child calls the parent's constructor itself, with parent::__construct($message).