Improving the `isLeft` branch can be achieved by using a more concise and expressive pattern matc...
is there a better way to do this
i want to keep the error handling where it is, but i'd like to avoid the exhaustive check with the if statements or be able to do something like the commented out Match
isLeftisLeft branch? i want to keep the error handling where it is, but i'd like to avoid the exhaustive check with the if statements or be able to do something like the commented out Match
import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
} from "@effect/platform";
import { Console, Effect, Either } from "effect";
const program = Effect.gen(function* () {
const client = yield* HttpClient.HttpClient;
const request = HttpClientRequest.get("https://httpbin.org/delay/5");
const response = yield* Effect.either(
client.execute(request).pipe(Effect.timeout("1 second"))
);
if (Either.isLeft(response)) {
// return yield* Match.value(response.left._tag).pipe(
// Match.when("RequestError", (error) => void),
// Match.when("ResponseError", (error) => void),
// Match.when("TimeoutException", (error) => void),
// Match.exhaustive
// );
if (response.left._tag === "RequestError") {
yield* Console.error("Request error occurred:", response.left);
return;
}
if (response.left._tag === "ResponseError") {
yield* Console.error("Response error occurred:", response.left);
return;
}
if (response.left._tag === "TimeoutException") {
yield* Console.error("Request timed out after 1 second");
return;
}
yield* Console.error("exhaustive", response.left);
return;
}
const json = yield* response.right.json;
yield* Console.log(json);
}).pipe(Effect.provide(FetchHttpClient.layer));
Effect.runPromise(program);import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
} from "@effect/platform";
import { Console, Effect, Either } from "effect";
const program = Effect.gen(function* () {
const client = yield* HttpClient.HttpClient;
const request = HttpClientRequest.get("https://httpbin.org/delay/5");
const response = yield* Effect.either(
client.execute(request).pipe(Effect.timeout("1 second"))
);
if (Either.isLeft(response)) {
// return yield* Match.value(response.left._tag).pipe(
// Match.when("RequestError", (error) => void),
// Match.when("ResponseError", (error) => void),
// Match.when("TimeoutException", (error) => void),
// Match.exhaustive
// );
if (response.left._tag === "RequestError") {
yield* Console.error("Request error occurred:", response.left);
return;
}
if (response.left._tag === "ResponseError") {
yield* Console.error("Response error occurred:", response.left);
return;
}
if (response.left._tag === "TimeoutException") {
yield* Console.error("Request timed out after 1 second");
return;
}
yield* Console.error("exhaustive", response.left);
return;
}
const json = yield* response.right.json;
yield* Console.log(json);
}).pipe(Effect.provide(FetchHttpClient.layer));
Effect.runPromise(program);