My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Exception naming and error code best practices ?

Adrien N.'s photo
Adrien N.
·May 26, 2016

What's your piece of advice on exception naming these days ?

  • Standard (Java-style, old?) convention would be to keep them at the root of your namespace with an Exception suffix: Example\Project\ConfigurationException
  • Symfony splits them by purpose/components (source):
    • Example\Project\User\BannedUserException
    • Example\Project\Graphs\NotEnoughDataException
  • Facebook store them and keep the Exception suffix to its classes: Facebook\Exceptions\FacebookSDKException.php (source)
  • Braintree keep all its exceptions together inside an Exception folder: Braintree\Exception\NotFound (source)
  • Stripe has ditched entirely the word Exception in his SDK and keep them together inside the same folder: Stripe\Error\InvalidRequest. (source)

There seems to be as much as ways as there's developers out there... but is there a written convention or what's the trend out there used by the most of us ?


Also, do you always set an error code as part of your exceptions ? If so, how do you manage those code ?

I've got a bunch of constants in Example\Project\Errors, just like Braintree does.