Handling Errors and Tracking Them in Effect TypeScript

Hi 👋 Looking for a bit of help on how I should approach this code. I have an array of responses, of which there might be an error in there (I actually only need the first value out of the response, as I'm only requesting one instead of a batch). I want to both track the errors, and recover gracefully, how would I handle this? I have a feeling I need to use the either type, alongside a tapError or partition. The consumer of this gen has a catchAll and is succeeding with a value that I can use on error. But I still want to track that error somewhere

const response = yield* fetchService
          .json(
            VisionApiResponse,
            `${url}:annotate?key=${Redacted.value(key)}`,
            {
              method: "POST",
              headers: {
                "Content-Type": "application/json",
              },
              body: JSON.stringify(buildRequestBody(imageBuffer)),
            },
          )
          .pipe(
            Effect.map((res) => res.responses.filter((r) => !r.error)), // <-- this swallows errors, not ideal
            Effect.flatMap((res) =>
              res.length === 0
                ? Effect.fail(
                    new Error("No responses received from Vision API"),
                  )
                : Effect.succeed(res[0]),
            ),
            Effect.flatMap((response) =>
              !!response
                ? Effect.succeed(response)
                : Effect.fail(
                    new Error("No responses received from Vision API"),
                  ),
            ),
          );
Was this page helpful?