Effect CommunityEC
Effect Community3y ago
2 replies
Great Britton

Different Error Channels and Same Value Channel in Services

Does a service have to have the same error channel and value channel to be the same or could I have a service that either contains errors A or error B and the same value ie for getting json with fs vs remote would have different possible errors but both return json string to be parsed?
 
export interface JSONDataSource {
  readonly _tag: "JSONDataSource";
  readonly getData: (
    path: string,
    fileType: string
  ) => Effect.Effect<never, Fetch | FSReadFile, string>;
}

export const JSONDataSource = Context.Tag<JSONDataSource>(
  "@backend/JSONDataSource"
);

export const parseJSON = (path: string, fileType: string) =>
  JSONDataSource.pipe(
    Effect.flatMap((_) => _.getData(path, fileType)),
    Effect.flatMap((data) =>
      Effect.try({
        try: () => JSON.parse(data) as Record<string, ValueType>,
        catch: (err) =>
          new Parse({
            error: (err as SyntaxError).message,
            type: `parsing ${path}`,
          }),
      })
    )
  );
Was this page helpful?