hicommonwealth / commonwealth

A platform for decentralized communities
https://commonwealth.im
GNU General Public License v3.0
65 stars 38 forks source link

Design event handler response conventions #7593

Open rbennettcw opened 2 weeks ago

rbennettcw commented 2 weeks ago

When policies or projections throw errors, they may need to be retried since they are not directly invoked by an actor.

Acceptance Criteria

rbennettcw commented 2 weeks ago

Event handler response conventions

// reusable strategy which retries on network failure
const networkFailStrategy = new RetryStrategy({
  'Network request failure': {
    maxRetries: 3,
    condition: async (err) => {
      const networkErrors = [
        'ECONNREFUSED', 'ETIMEDOUT', 'EHOSTUNREACH', 'ENETDOWN'
      ];
      return networkErrors.includes(error.code);
    },
  }
});

// multiple strategies can be applied like middleware
const result = await withRetryStrategy([networkFailStrategy], async () => {
  return contractHelpers.doThingThatThrows();
});

// any errors that aren't caught by retry policy = Block
// key = human readable error message, value = retry options
type RetryOptions = Record<string, {
  maxRetries: number;
  condition: (Error) => Promise<boolean>;
}>;
Rotorsoft commented 2 weeks ago

liking it đź’Ż ... let's discuss soon

rbennettcw commented 2 weeks ago

Updating the pattern to allow mapping any error to any strategy for future-proofing (exact naming convention TBD):

const networkFailStrategy = new Strategy({
  'Network request failure': {
    fn: async (err) => {
      const networkErrors = [
        'ECONNREFUSED', 'ETIMEDOUT', 'EHOSTUNREACH', 'ENETDOWN'
      ];
      if (networkErrors.includes(error.code)) {
        return new RetryStrategy({ maxRetries: 3 })
      }
      return null
    },
  }
});

Next steps:

Ticket 1 – Create reusable utility #7637

Ticket 2 – Implement in consumer #7638