Skip to content

question: getById vs findById and NoSuchElementException #28

@christophe-g

Description

@christophe-g

I find myself writing a bit of boilerplate code around NoSuchElementException, and I not sure if there a better way ...

Basically:

    const exportRepo = yield* makeExportRepo(entityId)
    const exportRecord = yield* exportRepo.getById(exportId)
      .pipe(Effect.catchTag('NoSuchElementException', () => Effect.succeed(Option.none())))
      .pipe(Effect.map(Option.getOrUndefined))

    if (exportRecord === undefined) {
      return yield* new ImportError({ message: `Export with ID ${exportId} not found` })
    }

There is a mention of findById somewhere in the docs, but it seems not yet implemented.

Is the plan to allow?

    const exportRecord = yield* exportRepo.findById(exportId)
    if (Option.isNone(exportRecord)) {
      return yield* new ImportError({ message: `Export with ID ${exportId} not found` })
    }

If yes, that would be great.

As a subsidiary question, is this the correct way to mock non-existing values FirestoreService.get for tests purposes?

I am doing this, with db object holding a couple of values needed for specific tests. I hope this is not what is causing the NoSuchElementException in the first place, but I am not sure how to mock the not found case:

const createFirestoreMock = (db: Record<string, any>) => Layer.mock(FirestoreService, {
  get: (path) => {
    const data = db[path]
    if (data) {
      const id = data.id || path.split('/').pop()!
      const meta = { id, path }
      return Effect.succeed(Option.some([meta, data]))
    }
    return Effect.succeed(Option.none()) // <----------------------------- How to mock not found case?
  },
  // ... other methods
})

And again, thanks a lot for open sourcing this !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions