PabloAndo99
2/8/2020 - 10:13 PM

02. Error Handling

For example, when using getTaskById method, the id selected could not match any of the tasks. NestJS allow us to handle this error by just simply adding the following:

  getTaskById(id: string): Task {
    const found = this.tasks.find(task => task.id === id);

    if (!found) {
      throw new NotFoundException(`Task with ID "${id}" not found`);
    }

    return found;
  }