Effect CommunityEC
Effect Community3y ago
33 replies
Sly

Best practices for narrowing down errors for specific treatment

What would be the best practice for differentiating between different error types in order to e.g. build a corresponding retry?

In https://gist.github.com/mikearnaldi/4a13fe6f51b28ad0b07fd7bbe3f4c49a we can see

export const retryPolicy = pipe(
  Schedule.exponential(Duration.millis(10)),
  Schedule.compose(Schedule.elapsed()),
  Schedule.whileOutput(Duration.lessThanOrEqualTo(Duration.seconds(10))),
  Schedule.whileInput(Schema.is(HttpError))
)


Based on:
const HttpError_ = Schema.struct({ _tag: Schema.literal("HttpError") })
export interface HttpError extends Schema.To<typeof HttpError_> {}
export const HttpError: Schema.Schema<HttpError> = Schema.to(HttpError_)

export const fetch200 = (url: string) =>
  Effect.tryCatchPromise(
    () => fetch(url).then((res) => res.status === 200 ? res.json() : Promise.reject()),
    () => identity<HttpError>({ _tag: "HttpError" })
  )


How would I adapt that to e.g. only retry in case of a more specific HttpError, e.g. 429? Would I rather adapt the fetch to throw more specific errors, or adapt the HttpError to somehow contain the status code and adapt the retry schedule based on that status code?
Gist
Generic batching & retries. GitHub Gist: instantly share code, notes, and snippets.
Generic batching & retries
Was this page helpful?