Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/grumpy-plums-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform": minor
---

support non-errors in HttpClient.retryTransient
35 changes: 27 additions & 8 deletions packages/platform/src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,20 +523,39 @@ export const retry: {
* @category error handling
*/
export const retryTransient: {
<B, E, R1 = never>(
<
B,
E,
R1 = never,
const Mode extends "errors-only" | "response-only" | "both" = never,
Input = "errors-only" extends Mode ? E
: "response-only" extends Mode ? ClientResponse.HttpClientResponse
: ClientResponse.HttpClientResponse | E
>(
options: {
readonly while?: Predicate.Predicate<NoInfer<E>>
readonly schedule?: Schedule.Schedule<B, NoInfer<E>, R1>
readonly mode?: Mode | undefined
readonly while?: Predicate.Predicate<NoInfer<Input>>
readonly schedule?: Schedule.Schedule<B, NoInfer<Input>, R1>
readonly times?: number
} | Schedule.Schedule<B, NoInfer<E>, R1>
} | Schedule.Schedule<B, NoInfer<Input>, R1>
): <R>(self: HttpClient.With<E, R>) => HttpClient.With<E, R1 | R>
<E, R, B, R1 = never>(
<
E,
R,
B,
R1 = never,
const Mode extends "errors-only" | "response-only" | "both" = never,
Input = "errors-only" extends Mode ? E
: "response-only" extends Mode ? ClientResponse.HttpClientResponse
: ClientResponse.HttpClientResponse | E
>(
self: HttpClient.With<E, R>,
options: {
readonly while?: Predicate.Predicate<NoInfer<E>>
readonly schedule?: Schedule.Schedule<B, NoInfer<E>, R1>
readonly mode?: Mode | undefined
readonly while?: Predicate.Predicate<NoInfer<Input>>
readonly schedule?: Schedule.Schedule<B, NoInfer<Input>, R1>
readonly times?: number
} | Schedule.Schedule<B, NoInfer<E>, R1>
} | Schedule.Schedule<B, NoInfer<Input>, R1>
): HttpClient.With<E, R1 | R>
} = internal.retryTransient

Expand Down
81 changes: 59 additions & 22 deletions packages/platform/src/internal/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import type * as Fiber from "effect/Fiber"
import * as FiberRef from "effect/FiberRef"
import { constFalse, dual } from "effect/Function"
import { constFalse, dual, flow, identity } from "effect/Function"
import { globalValue } from "effect/GlobalValue"
import * as Inspectable from "effect/Inspectable"
import * as Layer from "effect/Layer"
Expand Down Expand Up @@ -744,41 +744,76 @@ export const retry: {

/** @internal */
export const retryTransient: {
<B, E, R1 = never>(
<
B,
E,
R1 = never,
const Mode extends "errors-only" | "response-only" | "both" = never,
Input = "errors-only" extends Mode ? E
: "response-only" extends Mode ? ClientResponse.HttpClientResponse
: ClientResponse.HttpClientResponse | E
>(
options: {
readonly while?: Predicate.Predicate<NoInfer<E>>
readonly schedule?: Schedule.Schedule<B, NoInfer<E>, R1>
readonly mode?: Mode | undefined
readonly while?: Predicate.Predicate<NoInfer<Input>>
readonly schedule?: Schedule.Schedule<B, NoInfer<Input>, R1>
readonly times?: number
} | Schedule.Schedule<B, NoInfer<E>, R1>
} | Schedule.Schedule<B, NoInfer<Input>, R1>
): <R>(self: Client.HttpClient.With<E, R>) => Client.HttpClient.With<E, R1 | R>
<E, R, B, R1 = never>(
<
E,
R,
B,
R1 = never,
const Mode extends "errors-only" | "response-only" | "both" = never,
Input = "errors-only" extends Mode ? E
: "response-only" extends Mode ? ClientResponse.HttpClientResponse
: ClientResponse.HttpClientResponse | E
>(
self: Client.HttpClient.With<E, R>,
options: {
readonly while?: Predicate.Predicate<NoInfer<E>>
readonly schedule?: Schedule.Schedule<B, NoInfer<E>, R1>
readonly mode?: Mode | undefined
readonly while?: Predicate.Predicate<NoInfer<Input>>
readonly schedule?: Schedule.Schedule<B, NoInfer<Input>, R1>
readonly times?: number
} | Schedule.Schedule<B, NoInfer<E>, R1>
} | Schedule.Schedule<B, NoInfer<Input>, R1>
): Client.HttpClient.With<E, R1 | R>
} = dual(
2,
<E extends E0, E0, R, B, R1 = never>(
self: Client.HttpClient.With<E, R>,
options: {
readonly while?: Predicate.Predicate<NoInfer<E>>
readonly schedule?: Schedule.Schedule<B, NoInfer<E>, R1>
readonly mode?: "errors-only" | "response-only" | "both" | undefined
readonly while?: Predicate.Predicate<ClientResponse.HttpClientResponse | NoInfer<E>>
readonly schedule?: Schedule.Schedule<B, ClientResponse.HttpClientResponse | NoInfer<E>, R1>
readonly times?: number
} | Schedule.Schedule<B, NoInfer<E>, R1>
): Client.HttpClient.With<E, R | R1> =>
transformResponse(
} | Schedule.Schedule<B, ClientResponse.HttpClientResponse | NoInfer<E>, R1>
): Client.HttpClient.With<E, R | R1> => {
const isOnlySchedule = Schedule.ScheduleTypeId in options
const mode = isOnlySchedule ? "both" : options.mode ?? "both"
const schedule = isOnlySchedule ? options : options.schedule
const passthroughSchedule = schedule && Schedule.passthrough(schedule)
const times = isOnlySchedule ? undefined : options.times
return transformResponse(
self,
Effect.retry({
while: Schedule.ScheduleTypeId in options || options.while === undefined
? isTransientError
: Predicate.or(isTransientError, options.while),
schedule: Schedule.ScheduleTypeId in options ? options : options.schedule,
times: Schedule.ScheduleTypeId in options ? undefined : options.times
})
flow(
mode === "errors-only" ? identity : Effect.repeat({
schedule: passthroughSchedule,
times,
while: isOnlySchedule || options.while === undefined
? isTransientResponse
: Predicate.and(isTransientResponse, options.while)
}),
mode === "response-only" ? identity : Effect.retry({
while: isOnlySchedule || options.while === undefined
? isTransientError
: Predicate.or(isTransientError, options.while),
schedule,
times
})
)
)
}
)

const isTransientError = (error: unknown) =>
Expand All @@ -787,7 +822,9 @@ const isTransientError = (error: unknown) =>
const isTransientHttpError = (error: unknown) =>
Error.isHttpClientError(error) &&
((error._tag === "RequestError" && error.reason === "Transport") ||
(error._tag === "ResponseError" && error.response.status >= 429))
(error._tag === "ResponseError" && isTransientResponse(error.response)))

const isTransientResponse = (response: ClientResponse.HttpClientResponse) => response.status >= 429

/** @internal */
export const tap = dual<
Expand Down