Refactor Error Handling with Effect Functions for Sentry Reporting

I am trying to process request errors into a format that can be used within the app. The type that goes into catchAll is as follows: Effect<Data, NetworkOfflineError | HTTPError | TimeoutError | Error, never>

I created a tap function to send Sentry reports in problematic situations, as shown below:
function tapReportSentry<S>(data: unknown) {
  return Effect.tap<S, void>(() => {
    // send data
  });
}


The situations where Sentry reports will not be sent are as follows:
1. When the network is offline
2. When the data received as a result of a failed response matches the schema allowed by the app

I wrote the following code; is it possible to refactor this further using Effect functions?
Effect.catchAll(error => {
      if (error instanceof NetworkOfflineError) {
        // no report
        return Effect.succeed({
          message: CommonRequestErrorMessage,
          success: false as const,
        });
      }

      if (error instanceof HTTPError) {
        return Effect.tryPromise<RequestError>(() => error.response.json()).pipe(
          Effect.flatMap(data =>
            data.description
              ? Effect.succeed(data.description)
              : Effect.succeed(CommonRequestErrorMessage).pipe(tapReportSentry(error)),
          ),
          Effect.map(message => ({
            message,
            success: false as const,
          })),
        );
      }

      // TimeoutError or unexpected error
      const result = Effect.succeed({
        message: CommonRequestErrorMessage,
        success: false as const,
      }).pipe(tapReportSentry(error));

      return result;
    })
Was this page helpful?