Effect CommunityEC
Effect Community4w ago
12 replies
fstodulski

Handling ORPCError with Effect and Drizzle in Client-Server Communication

Good evening gents.
Looking for a help.
Using Effect (with drizzle) + ORPC to get data on my client.

How can I use effect, but still throw ORPCError so the client can't catch it properly?
  db: publicProcedure.handler(async () => {
    const program = Effect.gen(function* () {
      const db = yield* DbService;

      return yield* Effect.tryPromise({
        try: () => {
          throw new DatabaseError({
            message: "Failed to find services in db",
          });
        },
        catch: () =>
          new DatabaseError({
            message: "Catched during tryPromise",
          }),
      });
    }).pipe(Effect.provide(DbService.live));

    const result = await Effect.runPromise(
      program.pipe(
        Effect.catchTags({
          DatabaseError: (error) =>
            Effect.fail(
              new ORPCError("NOT_FOUND", {
                message: "Test",
              })
            ),
        })
      )
    );

    return result;
  }),


Client:
  const { data: db, error, isError } = useQuery(orpc.db.queryOptions());
  console.log(
    "result>>",
    JSON.stringify(
      {
        db,
        error,
        isError,
      },
      null,
      2
    )
  );
  useEffect(() => {
    if (isError) {
      console.log("error>>", error.message);
      toast.error(error.message);
    }
  }, [isError, error]);


Response:
{
    "json": {
        "defined": false,
        "code": "INTERNAL_SERVER_ERROR",
        "status": 500,
        "message": "Internal server error"
    }
}


Client:
 →  result>> {
  "error": {
    "defined": false,
    "code": "INTERNAL_SERVER_ERROR",
    "status": 500,
    "message": "Internal server error"
  },
  "isError": true
}
Was this page helpful?