Declarative Error Handling with Effect RN

I've always struggled with expressing recoverable errors in a meaningful and declarative way. I've had this ah-ha moment with effect rn. Wanted to know if you find this pattern interesting:
export class FileExistsError extends Data.TaggedClass("FileExistsError")<{
  readonly path: string;
  readonly contents: string;
  readonly options?: FsOptions;
}> {
  overwrite = Effect.asUnit(Effect.all(
    Effect.promise(() => removeFile(this.path, this.options)),
    Effect.promise(() => writeFile(this.path, this.contents, this.options)),
  ));
}

// FileService Service ¯\_(ツ)_/¯
export const tag = Context.Tag<Fs, FsService>("FsService");

export const live = Layer.sync(
  tag,
  () => {
    return {
      writeFile: (path, contents, options?) =>
        Effect.ifEffect(
          Effect.promise(() => exists(path, options)),
          Effect.fail(new FileExistsError({ path, contents, options })),
          Effect.promise(() => writeFile(path, contents, options)),
        ),
    };
  },
);
Was this page helpful?