We’re all told that our exception messages should be nice and informative.
Often sprintf is used to format the exception messages. This is all well and
good, it’s very helpful when an exception gets thrown and logged. However, it
leads to code that looks like this:
This looks like a bit of a mess to me. It takes up 9 lines and has 3 extra
levels of indentation.
Some time ago I submitted a pull request on a project (I forget which) and
Ocramius suggested that I use a static factory
method inside my exception to make it neater. I had a look at some exception
classes in the project and definitely found it was neater! (I assume others are
out there using this pattern too?).
I decided to adopt this pattern, and instead of the code above I now had this:
Exception Class
Code
So far this has certainly tidied up the code throwing the exception, and made
it much easier to see what’s going on. Also, all the exception messages are now
in 1 place, inside the exception class, so they’re easier to manage.
However, at this point I still think that the exception class looks a bit messy
and could be improved further. Using the wonders of PHP 5.4’s nice new feature,
I created the following trait:
Now, by telling my exception class to use this trait, I can have nice neat
exception classes like so:
I think is a much more elegant solution!
Please let me know of any thoughts or suggestions!