-
Notifications
You must be signed in to change notification settings - Fork 1
4. Error Handling
Chabardes edited this page May 25, 2022
·
20 revisions
As we said in the previous chapter, we don't check much in the command handler. Does the Hero exists ? Does the Item exists ? Does the Item being equipped belongs to the Hero ?
At Libeo, we use the neverthrow library. The goal is to explicitly tell what could go wrong in our handlers. You can find more information on how we handle our backend errors in the Notion documentation.
- Create a
HeroDoesNotOwnItemError in our domainheroes/core/domain/hero.error.ts. - Create a
EquipItemCommandResultwith a neverthrow Result and explicit the expected errors inheroes/core/application/commands/equip-item/equip-item.command.ts. - Use the decorators
@WrapInTryCatchWithUnknownApplicationErrorand@LogPayloadAndResultin the handlerheroes/core/application/commands/equip-item/equip-item.command-handler.tsto catch and log any unexpected errors. - Update the command handler to return the desired
EquipItemCommandResult. Remember, we should not have to throw/catch anything here. Check for anything that could go wrong and use theerr()method fromneverthrowto return an expected Error.
- Get the result from the command in the
equipItemmutation inheroes/interface/graphql/hero.resolver.ts - If the result is an error, get the kind of error with
result.error.constructorand throw the correctHttpExceptionwith the error message; We don't want the full error with the stacktrace to go to the client.