import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Option from "effect/Option";
import {
deploymentFailure,
isDeploymentFailure,
TemporarilyUnavailable,
type DeploymentCode,
type DeploymentFailure,
} from "./deployment-errors.ts";
import {
domainErrorCode,
type DomainErrorCode,
type DomainFailure,
} from "./domain-errors.ts";
import type { RegistryFailure } from "./installation-errors.ts";
// Only declared codes cross native Workflow persistence/retry boundaries.
// Provider bodies, RPC causes and credentials never become Workflow history.
export async function runDeploymentEffect(
program: Effect.Effect,
): Promise {
const exit = await Effect.runPromiseExit(program);
if (Exit.isSuccess(exit)) return exit.value;
const error = Exit.findErrorOption(exit);
throw Option.isSome(error) && isDeploymentFailure(error.value)
? deploymentFailure(error.value.code)
: new TemporarilyUnavailable();
}
export async function runDeploymentStep(
program: Effect.Effect,
): Promise<
| { complete: true; errorCode: null }
| { complete: false; errorCode: DeploymentCode }
> {
try {
await runDeploymentEffect(program);
return { complete: true, errorCode: null };
} catch (error) {
if (isDeploymentFailure(error) && error.code !== "temporarily_unavailable")
return { complete: false, errorCode: error.code };
throw new Error("temporarily_unavailable");
}
}
export function domainExitCode(
exit: Exit.Failure,
): DomainErrorCode {
const error = Exit.findErrorOption(exit);
return Option.isSome(error)
? domainErrorCode(error.value)
: "temporarily_unavailable";
}
export async function runDomainStep(
program: Effect.Effect,
): Promise<{ error: DomainErrorCode | null }> {
const exit = await Effect.runPromiseExit(program);
if (Exit.isSuccess(exit)) return { error: null };
const code = domainExitCode(exit);
if (code === "temporarily_unavailable") throw new Error(code);
return { error: code };
}