Page MenuHomePhabricator

Should our use cases throw exceptions instead of returning different response objects?
Closed, ResolvedPublic

Description

Currently, our UseCases can return three different types of response objects:

  1. A "success" response (e.g. GetItemLabelsSuccessResponse)
  2. An ItemRedirectResponse
  3. An ErrorResponse (e.g. GetItemLabelsErrorResponse)

While discussing Chapter 9: Validation of Advanced Web Application Architecture in our book club, we decided it would be useful to try out only returning a success response object from a UseCase and throwing an exception in the other situations.

We perceive that the benefits of throwing exceptions would be:

  • The UseCase execute() method would only need a single return type instead of union types
  • In the RouteHandlers, catching different exceptions is nicer than doing several instanceof checks in if/elseif/else (or switch) statements
  • Checks like "is there a validation error", "does the item exist", "is the item a redirect", or "does the user have permission" can be condensed into a few method calls, reducing the number of lines in the UseCase execute() method

It was left open as to whether we would try this out on existing code (e.g. the GetItemLabels UseCase as it is only used be a single RouteHandler) or to try it out on the next endpoint we implement.


Things to consider once a decision has been made:

  • Should we write an ADR explaining our decision?
  • Can we abstract some of the shared logic that is repeated in multiple UseCases?

Related Objects

StatusSubtypeAssignedTask
ResolvedMuhammad_Yasser_Jazirahly_WMDE
ResolvedWMDE-leszek
ResolvedOllie.Shotton_WMDE
ResolvedSilvan_WMDE
ResolvedMuhammad_Yasser_Jazirahly_WMDE
ResolvedOllie.Shotton_WMDE
ResolvedMuhammad_Yasser_Jazirahly_WMDE
ResolvedOllie.Shotton_WMDE
ResolvedMuhammad_Yasser_Jazirahly_WMDE
ResolvedSilvan_WMDE
ResolvedJakob_WMDE
ResolvedMuhammad_Yasser_Jazirahly_WMDE
ResolvedJakob_WMDE
ResolvedWMDE-leszek
ResolvedMuhammad_Yasser_Jazirahly_WMDE
ResolvedMuhammad_Yasser_Jazirahly_WMDE
ResolvedSilvan_WMDE
ResolvedMuhammad_Yasser_Jazirahly_WMDE
ResolvedSilvan_WMDE
ResolvedMuhammad_Yasser_Jazirahly_WMDE
ResolvedMuhammad_Yasser_Jazirahly_WMDE
ResolvedMuhammad_Yasser_Jazirahly_WMDE
ResolvedSilvan_WMDE

Event Timeline

Restricted Application added a subscriber: Aklapper. ยท View Herald TranscriptJan 20 2023, 4:54 PM

Change 889491 had a related patch set uploaded (by Muhammad Jaziraly; author: Muhammad Jaziraly):

[mediawiki/extensions/Wikibase@master] REST: Add ADR 8 about using exceptions in UseCases

https://gerrit.wikimedia.org/r/889491

Change 889491 merged by jenkins-bot:

[mediawiki/extensions/Wikibase@master] REST: Add ADR 8 about using exceptions in UseCases

https://gerrit.wikimedia.org/r/889491