Handling ParseError in Effect Schema: Service vs. Detached Approach

I have a set of services, including a service that fetches some content via API. This service has a set of methods like getAllOrders and getOrderById which parses the json response through Effect.Schema and returns the content in an Effect. There was an issue with a type in the schema which resulted in Effect.Schema crashing with a ParseError.

Is it better (or recommended) to handle the error in the method within the service like;

getAllOrders: Effect.gen(function* (_) {
  const response = yield* Effect.tryPromise({
    try: () =>
      fetch(API_ENDPOINT, {
        headers: headers,
      }),
    catch: () => new FetchError({message: "Failed to fetch orders"}),
  });
  const jsonResp = yield* Effect.promise(() => response.json());
  const orders = Schema.decodeUnknown(Schema.Array(Order))(jsonResp, {
    errors: "all",
  });
  return yield* orders;
}).pipe(
  Effect.catchTag("ParseError", (e) =>
    Effect.log(`Failed to parse orders: ${e.message}`)
  )
);


or detached from the service like;

const program = Effect.gen(function* () {
  console.log("Running program...");
  const WooAPI = yield* WooAPIService;
  // const orders = yield* WooAPI.getOrderById("724004");
  const orders = yield* WooAPI.getAllOrders;
  // const orders = yield* WooAPI.createOrder;
  console.log(orders);
})

// handle errors
const runnable = program.pipe(
  Effect.catchTags({
    ConfigError: (error) => Effect.log("There was a failure with the ConfigService: \n", error),
    FetchError: (error) => Effect.log("There was a failure parsing fetching data: \n", error),
    ParseError: (error) => Effect.log("There was a failure parsing Schema:", error),
  }),
);
Was this page helpful?