Skip to content

Commit 9ab7e8e

Browse files
REST: Add ADR 8 about using exceptions in UseCases
Throw exceptions instead of returning response objects in use cases Bug: T327527 Change-Id: I0f3a648f27ab57da144079a3847780810ed82239
1 parent f47d503 commit 9ab7e8e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 8) Throw exceptions instead of returning response objects in use cases {#rest_adr_0008}
2+
3+
Date: 2023-01-20
4+
5+
## Status
6+
7+
Accepted
8+
9+
## Context
10+
11+
Currently, our UseCases can return three different types of response objects:
12+
13+
1. A "success" response (e.g. `GetItemLabelsSuccessResponse`)
14+
2. An `ItemRedirectResponse`
15+
3. An `ErrorResponse` (e.g. `GetItemLabelsErrorResponse`)
16+
17+
While discussing Chapter 9: Validation of `Advanced Web Application Architecture`, 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**.
18+
19+
We perceive that the benefits of throwing exceptions would be:
20+
21+
1. The UseCase `execute()` method would only need a single return type instead of union types.
22+
2. In the RouteHandlers, catching different exceptions is nicer than doing several `instanceof` checks in `if`/`elseif`/`else` (or `switch`) statements.
23+
3. Checks like <em>"is there a validation error"</em>, <em>"does the item exist"</em>, <em>"is the item a redirect"</em>, or <em>"does the user have permission"</em> can be condensed into a few method calls, reducing the number of lines in the UseCase `execute()` method.
24+
25+
## Considered Actions
26+
27+
### 1) Leave the current response objects as is
28+
29+
Keep current `ErrorResponse` and `ItemRedirectResponse`, without throwing exceptions.
30+
31+
### 2) Switch to the new approach of using exceptions
32+
33+
By trying to use exceptions instead of response objects in either an already implemented UseCase with a single `RouteHandler` like `GetItemLabels`, or when creating a new UseCase e.g. `GetItemDescriptions` or `GetItemAliases`.
34+
35+
## Decision
36+
37+
We decided to go with action 2: switch to throwing use case exceptions instead of returning different kinds of response objects.
38+
39+
## Consequences
40+
41+
- Modify old **use cases** and **validators** to use exceptions instead of response objects.
42+
- Since there is now only one response for each use case (the "SuccessResponse"), we will rename it to just "Response". (e.g. `GetItemLabelsSuccessResponse` -> `GetItemLabelsResponse`).

repo/rest-api/docs/adr/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ Current ADRs include:
1212
* @subpage rest_adr_0005
1313
* @subpage rest_adr_0006
1414
* @subpage rest_adr_0007
15+
* @subpage rest_adr_0008

0 commit comments

Comments
 (0)