decodeUnknownExit

Avatar of Hemanta SundarayHemanta Sundaray

decodeUnknownExit is a synchronous decoder that returns an Exit value instead of throwing.

Exit is an Effect data structure for representing outcomes explicitly: either Success (with a value) or Failure (with a cause).

  • Exit.Success contains the decoded value.
  • Exit.Failure contains the decode error (a SchemaError) in the cause.
schema.ts
import { Cause, Exit, Schema } from "effect";
const schema = Schema.Struct({
name: Schema.String,
age: Schema.Number,
});
const decode = Schema.decodeUnknownExit(schema);
// Happy path
const ok = decode({ name: "Alice", age: 30 });
if (Exit.isSuccess(ok)) {
console.log("Decoded Value:", ok.value);
}
// Error path
const bad = decode({ name: 42, age: "30" });
if (Exit.isFailure(bad)) {
console.log("Error Message:", Cause.pretty(bad.cause));
}

In the example above, I use Cause.pretty(bad.cause) to turn the Effect Cause (a structured error tree) into a human-readable string for logs.

Sign in to save progress

Stay in the loop

Get notified when new chapters are added and when this course is complete.