One Small Real Program in Effect (Learning Effect, Part 11)

By Esteban Trillo2 min readupdated Learning EffectEffectTypeScript

Parse, fetch, handle. Small gears that fit, wound up and run once.
Parse, fetch, handle. Small gears that fit, wound up and run once.

Now I want one small program.

Not a production app.

Not an architecture diagram.

Just enough code to see the pieces touch.

The flow

The program will:

One request through the programInside the GET handler, which is the edge, the program reads a user id from the request, parses it, and fetches the user. A found user becomes a 200 JSON response. If parsing fails, catchTag turns InvalidUserId into a 400. If the fetch fails, catchTag turns UserNotFound into a 404. The whole thing runs once, at the edge.GET handler: the edgeRequest?id=1parseUserIdfetchUser200user as JSON400InvalidUserId404UserNotFoundidusercatchTagcatchTag

That is a real shape.

The plain version

export async function GET(request: Request) {
const input = new URL(request.url).searchParams.get("id") ?? "";
try {
const id = parseUserId(input);
const user = await fetchUser(id);
return Response.json(user);
} catch (error) {
if (error instanceof UserNotFound) {
return new Response("User not found", { status: 404 });
}
if (error instanceof InvalidUserId) {
return new Response("Invalid user id", { status: 400 });
}
throw error;
}
}

It works.

It is also the version most people write first.

Nothing checks that list.

If fetchUser learns a new way to fail, this still compiles. The new error falls through to throw error.

The core program

const getUserResponse = (input: string) =>
parseUserId(input).pipe(
Effect.flatMap(fetchUser),
Effect.map((user) => Response.json(user)),
Effect.catchTag("UserNotFound", () =>
Effect.succeed(new Response("User not found", { status: 404 })),
),
);

Read it straight down.

Parse.

Fetch.

Turn success into JSON.

Handle one known error.

Which error is handled here?

The type tells the truth

After handling UserNotFound, the type might still say:

Effect.Effect<Response, InvalidUserId, UserRepo>;

That means:

  • success: Response
  • error: InvalidUserId
  • requirement: UserRepo

There is still work to do before running it.

We need to decide what bad input means.

We need to provide UserRepo.

What still must be provided before this program can run?

Run it at the edge

export async function GET(request: Request) {
const id = new URL(request.url).searchParams.get("id") ?? "";
const program = getUserResponse(id).pipe(
Effect.provide(UserRepoLive),
Effect.catchTag("InvalidUserId", () =>
Effect.succeed(new Response("Invalid user id", { status: 400 })),
),
);
return Effect.runPromise(program);
}

The runner appears once.

At the edge.

That feels right.

The mistake: running with errors left over

Effect.runPromise accepts a program with errors still in the slot.

If one of them happens, the promise rejects.

At the edge, that usually means a 500.

Before you run, hover program. Here it should read:

Effect.Effect<Response, never, never>;

Nothing left to handle. Nothing left to provide.

If the error slot is not never, decide what each error means first.

A 400, a 404, a retry, or a log line.

Each one should be a choice you can see.

What I learned from the first pass

Effect is not one idea.

It is a stack of small ideas:

  • describe work before running it
  • make errors visible
  • make dependencies visible
  • compose the happy path without losing the failure path
  • run the program at the edge

I still have a lot to learn.

But now I can read the first line of the map.

Effect.Effect<Success, Error, Requirements>;

That is enough to keep going.

Piwi hands a single letter to the user at a mailbox; a small note reads no noise.

Get the next one in your inbox

One email when a post goes out, with its sketch. Nothing in between, and one link to leave.

Sign in faster next time?

Add a passkey and skip the email link. Your fingerprint, face or device PIN is enough.