Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

orchestration.ts

54 lines1.9 KB

1import {
2 EnvironmentResourceNotFoundError,
3 OrchestrationDispatchCommandError,
4 OrchestrationThreadNotFoundError,
5} from "@t3tools/contracts";
6import * as Cause from "effect/Cause";
7import * as Schema from "effect/Schema";
8​
9export type TerminalThreadNotFoundError =
10 | EnvironmentResourceNotFoundError
11 | OrchestrationThreadNotFoundError;
12​
13const isEnvironmentResourceNotFoundError = Schema.is(EnvironmentResourceNotFoundError);
14const isOrchestrationDispatchCommandError = Schema.is(OrchestrationDispatchCommandError);
15const isOrchestrationThreadNotFoundError = Schema.is(OrchestrationThreadNotFoundError);
16​
17function isHttpThreadNotFoundError(error: unknown): error is EnvironmentResourceNotFoundError {
18 return isEnvironmentResourceNotFoundError(error) && error.reason === "thread_not_found";
20​
21export function isTerminalThreadNotFoundError(
22 error: unknown,
23): error is TerminalThreadNotFoundError {
24 return isHttpThreadNotFoundError(error) || isOrchestrationThreadNotFoundError(error);
26​
27export function findHttpThreadNotFoundError(
28 cause: Cause.Cause<unknown>,
29): EnvironmentResourceNotFoundError | undefined {
30 for (const reason of cause.reasons) {
31 if (Cause.isFailReason(reason) && isHttpThreadNotFoundError(reason.error)) {
32 return reason.error;
33 }
34 }
35 return undefined;
37​
38export function hasTerminalThreadNotFoundFailure(cause: Cause.Cause<unknown>): boolean {
39 return cause.reasons.some(
40 (reason) => Cause.isFailReason(reason) && isTerminalThreadNotFoundError(reason.error),
41 );
43​
44export function wasBootstrapThreadDeleted(error: unknown): boolean {
45 return (
46 isOrchestrationDispatchCommandError(error) && error.bootstrapThreadDisposition === "deleted"
47 );
49​
50export function wasBootstrapThreadNotCreated(error: unknown): boolean {
51 return (
52 isOrchestrationDispatchCommandError(error) && error.bootstrapThreadDisposition === "not-created"
53 );