Effect CommunityEC
Effect Community3y ago
36 replies
Victor Korzunin

Request for Help with Unexpected Function Behavior

Hello guys, I have some not expected behavior, and cannot figure out myself, could you please help. So I have this function
const CollectionArgsTag = Context.Tag<CollectionArgs>();

export const findOne = <TSchema extends Document = Document>(
  filter: Filter<TSchema>
) =>
  Effect.flatMap(Database.DatabaseTag, (db) =>
    Effect.flatMap(CollectionArgsTag, (collectionArgs) =>
      Effect.tryCatchPromise(
        () => db.collection<TSchema>(...collectionArgs).findOne(filter),
        (error) => {
          if (error instanceof MongoInvalidArgumentError) {
            return new MongoInvalidArgumentTaggedError(error);
          }

          throw error;
        }
      )
    )
  );

const findOneConcreteDocument = flow(
  findOne<ConcreteDocument>,
  Effect.provideService(CollectionArgsTag, ["concretes"])
);

const program = pipe(
  findOneConcreteDocument({ _id: "whatever" }),
  Effect.provideSomeLayer(Database.MongoDbImpl),
  Effect.runPromise
);
where Database is a namespace for DatabaseTag and corresponding MongoDbImpl which works fine. However the problem is in CollectionArgsTag: in local tests all work perfect, but in runtime I get error
2023-05-14T22:48:37.776Z    d232448b-220c-4f3f-a907-7a4d7361fdba    INFO    TagImpl2 {
  _tag: 'Tag',
  i0: undefined,
  i1: undefined,
  i2: undefined,
  trace: undefined,
  [Symbol(@effect/io/Effect)]: { _R: [Function: _R], _E: [Function: _E], _A: [Function: _A] }
}
which apparently means no service provided for the tag. But as you can see the service is provided and all type checks are perfect.
Was this page helpful?