Effect CommunityEC
Effect Community3w ago
2 replies
fstodulski

Simplifying Error Handling with Effect and ORPC

Is there a simplier way to use Effect with ORPC? My goal is to handle errors via Effect, but throw them to the ORPC so the client can display them.

export const updateCustomer = businessProcedure
  .input(Schema.standardSchemaV1(UpdateCustomerInputSchema))
  .handler(async ({ input }) => {
    const exit = await AppRuntime.runPromiseExit(
      Effect.gen(function* () {
        const customersQuery = yield* CustomersQuery;
        return yield* customersQuery.updateCustomer(input);
      }).pipe(
        Effect.catchTags({
          SQLCustomerError: (error) =>
            Effect.fail(
              new ORPCError("INTERNAL_SERVER_ERROR", {
                message: error.message,
                cause: error.cause,
              })
            ),
          CustomerNotFoundError: (error) =>
            Effect.fail(
              new ORPCError("NOT_FOUND", {
                message: error.message,
                cause: error.cause,
              })
            ),
          CustomerTransactionError: (error) =>
            Effect.fail(
              new ORPCError("INTERNAL_SERVER_ERROR", {
                message: error.message,
                cause: error.cause,
              })
            ),
          SQLTransactionError: (error) =>
            Effect.fail(
              new ORPCError("INTERNAL_SERVER_ERROR", {
                message: error.message,
                cause: error.cause,
              })
            ),
        })
      )
    );

    return getDataOrThrowRawError(exit);
  });
Was this page helpful?