tempest-framework icon indicating copy to clipboard operation
tempest-framework copied to clipboard

Document standards for exceptions

Open aidan-casey opened this issue 1 year ago • 1 comments

We have a few standards going on for exception messages. It would be good to document and enforce our standards.

My rough initial proposal would be:

  1. Take inspiration from Microsoft
  2. Write grammatically correct sentences with punctuation.
  3. Include helpful feedback for what might be wrong.

If we can figure out using CI for this too.......... that'd be amazing. 😍

aidan-casey avatar Sep 28 '24 19:09 aidan-casey

Agree! We also need to replace all default exceptions to custom ones with proper messages.

brendt avatar Oct 01 '24 03:10 brendt

@brendt I think we should do this before 1.0 and rename all exceptions to either be suffixed with Exception or not, currently we do both in different places

innocenzi avatar Jun 26 '25 09:06 innocenzi

So:

  • Agree on using proper punctuation and better sentences
  • IDK how I feel about ALWAYS using Exception as a suffix or NEVER using it. IMO, it makes sense in some cases but not always

brendt avatar Jun 26 '25 09:06 brendt

Standards we settled on (still need to document):

  • Exception classes should be thought of as events: start with the subject, then the verb in the past tense. <something> <happened>: DatabaseOperationFailed, StorageUsageWasForbidden, AuthenticatedUserWasMissing, etc.
  • Never suffix with Exception as it's always clear from the context that this is an exception (either with throw or catch)
  • Always extend \Exception
  • Preferably, provide an interface that specific exceptions can extend from. Eg. CacheException, DatabaseException, FilesystemException. These interfaces must be suffixed with Exception
  • Preferably, overwrite the constructor and only accept the input that's relevant
  • Write the exception message in the exception class, not from the context where it was thrown

brendt avatar Jun 26 '25 11:06 brendt

Ok so with #1308 merged, we need to review the docs:

  • [x] Update the docs with new exception names (I reckon a search for "catch", and "Exception" will probably highlight most classes
  • [x] Document the new standards (see https://github.com/tempestphp/tempest-framework/issues/499)

@innocenzi will look at it later today

brendt avatar Jun 26 '25 12:06 brendt

We only miss updating exception messages, which can be done over time.

https://tempestphp.com/main/features/exception-handling#customizing-the-error-page https://tempestphp.com/main/extra-topics/contributing#exception-classes

innocenzi avatar Jun 26 '25 14:06 innocenzi