|
1 | | -from typing import Callable, Dict, Optional, Type |
| 1 | +from typing import Any, Callable, Dict, List, Optional, Sequence, Type |
2 | 2 |
|
3 | | -from aws_doc_sdk_examples_tools.lliam.domain import commands |
| 3 | +from aws_doc_sdk_examples_tools.lliam.domain import commands, errors |
4 | 4 | from aws_doc_sdk_examples_tools.lliam.service_layer import ( |
5 | 5 | create_prompts, |
6 | 6 | dedupe_reservoir, |
|
13 | 13 | Message = commands.Command |
14 | 14 |
|
15 | 15 |
|
16 | | -def handle(message: commands.Command, uow: Optional[unit_of_work.FsUnitOfWork] = None): |
17 | | - queue = [message] |
| 16 | +def handle( |
| 17 | + message: Any, uow: Optional[unit_of_work.FsUnitOfWork] = None |
| 18 | +) -> Sequence[errors.DomainError]: |
| 19 | + if isinstance(message, commands.Command): |
| 20 | + return handle_command(message, uow) |
18 | 21 |
|
19 | | - while queue: |
20 | | - message = queue.pop(0) |
21 | | - if isinstance(message, commands.Command): |
22 | | - return handle_command(message, uow) |
23 | | - else: |
24 | | - raise Exception(f"{message} was not a Command") |
| 22 | + return [ |
| 23 | + errors.CommandExecutionError( |
| 24 | + command_name="Unknown", message=f"{message} was not a Command" |
| 25 | + ) |
| 26 | + ] |
25 | 27 |
|
26 | 28 |
|
27 | | -def handle_command(command: commands.Command, uow: Optional[unit_of_work.FsUnitOfWork]): |
28 | | - handler = COMMAND_HANDLERS[type(command)] |
29 | | - errors = handler(command, uow) |
30 | | - return errors |
| 29 | +def handle_command( |
| 30 | + command: commands.Command, uow: Optional[unit_of_work.FsUnitOfWork] |
| 31 | +) -> Sequence[errors.DomainError]: |
| 32 | + handler = COMMAND_HANDLERS.get(type(command)) |
| 33 | + if not handler: |
| 34 | + return [ |
| 35 | + errors.CommandExecutionError( |
| 36 | + command_name=command.name, message="Handler for not found." |
| 37 | + ) |
| 38 | + ] |
| 39 | + return handler(command, uow) |
31 | 40 |
|
32 | 41 |
|
33 | | -COMMAND_HANDLERS: Dict[Type[commands.Command], Callable] = { |
| 42 | +COMMAND_HANDLERS: Dict[ |
| 43 | + Type[commands.Command], Callable[..., Sequence[errors.DomainError]] |
| 44 | +] = { |
34 | 45 | commands.CreatePrompts: create_prompts.create_prompts, |
35 | 46 | commands.RunAilly: run_ailly.handle_run_ailly, |
36 | 47 | commands.UpdateReservoir: update_doc_gen.handle_update_reservoir, |
|
0 commit comments