Handling Specific Errors in Computations with Effect Library

I just discovered this wonderful library and starting learning how to use it. However, I am already stumped at what to do, if I want to match a Cause to a specific error when I have run a computation:

See the following example:

import { Effect, Exit } from 'effect';

class NegativeNumberError {
  readonly _tag = 'NegativeNumberError';
  readonly message = 'Number is negative';
}

class ZeroNumberError {
  readonly _tag = 'ZeroNumberError';
}

export const naturalNumber = (n: number) =>
  Effect.gen(function* (_) {
    if (n < 0) {
      yield* _(Effect.fail(new NegativeNumberError()));
    } else if (n === 0) {
      yield* _(Effect.fail(new ZeroNumberError()));
    }
    return n;
  });

Effect.runPromiseExit(naturalNumber(-1)).then(
  Exit.match({
    onSuccess: (n) => {
      console.log(`Number ${n} is natural.`);
    },
    onFailure: (cause) => {
      // What to do here to print specific error?
      // And how to print the message of the NegativeNumberError class for that given match
    },
  }),
);


Any help would be greatly appreciated!
Was this page helpful?