Effect CommunityEC
Effect Community3y ago
7 replies
cappie013

Debugging Effect.retry in JavaScript

Hello! When working with Effect.retry, is there a way to "debug" the retry and the reason why it's retrying? Here's my code:

F.pipe(
    Effect.flatMap((url) =>
      F.pipe(
        Effect.tryCatchPromise(
          () =>
            axios.get<ArrayBuffer>(
              `https://my-endpoint.com/{encodeURI(
                url
              )}`,
              { responseType: 'arraybuffer' }
            ),
          (e) => {
            return {
              errorCode: 'cannot-resize-image',
              message: getErrorMessage(e),
              shouldRetry: axios.isAxiosError(e) && e.response?.status !== 415,
            } as const;
          }
        ),
        Effect.retry(
          F.pipe(
            // Number of retries
            Schedule.recurs(5),
            // Delay between each retry
            Schedule.addDelay(() => Duration.millis(500)),
            // Retry until a condition is met
            Schedule.whileInput((error) => {
              console.log('retrying');
              return (
                isObject(error) && 'shouldRetry' in error && !!error.shouldRetry
              );
            })
          )
        ),
        Effect.tap(() => Effect.logDebug('foo')),

        Effect.orElse(() => {
          console.log(
            'cannot resize image, fallback to downloading original',
            url
          );
          return Effect.tryCatchPromise(
            () => axios.get<ArrayBuffer>(url, { responseType: 'arraybuffer' }),
            (e) =>
              ({
                errorCode: 'cannot-resize-image',
                message: getErrorMessage(e),
              } as const)
          );
        })
      )
    ),
    Effect.flatMap((obj) => {
      const hashSum = crypto.createHash('sha256');
      hashSum.update(obj.buffer);

      return Effect.succeed({
        ...obj,
        hash: hashSum.digest('hex'),
      });
    }),
    Effect.orElseSucceed(() => ({ ...obj, buffer: null, hash: null } as const))
  );
Was this page helpful?