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
},
}),
);
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
},
}),
);