Skip to content

4. Error Handling

Chabardes edited this page May 31, 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.

Core

  1. Create a HeroDoesNotOwnItem Error in our domain heroes/core/domain/hero.error.ts.
  2. Create a EquipItemCommandResult with a neverthrow Result and explicit the expected errors in heroes/core/application/commands/equip-item/equip-item.command.ts.
  3. Use the decorators @WrapInTryCatchWithUnknownApplicationError and @LogPayloadAndResult in the handler heroes/core/application/commands/equip-item/equip-item.command-handler.ts to catch and log any unexpected errors.
  4. 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 the err() method from neverthrow to return an expected Error.

Commit your changes

Interface

  1. Get the result from the command in the equipItem mutation in heroes/interface/graphql/hero.resolver.ts
  2. If the result is an error, get the kind of error with result.error.constructor and throw the correct HttpException with the error message; We don't want the full error with the stacktrace to go to the client.

Last commit

image

Clone this wiki locally