Open History to see the last commit
1import {
2 EnvironmentId,
3 type PreviewAutomationHost,
4 PreviewAutomationOperation,
5 PreviewAutomationRecordingTransferError,
6 PreviewAutomationRecordingDesktopUpdateRequiredError,
7 PreviewAutomationRecordingTooLargeError,
8 PreviewAutomationRecordingDeadlineExpiredError,
9 type PreviewAutomationRequest,
10 type PreviewAutomationResponse,
11 PreviewTabId,
12 ThreadId,
13 TrimmedNonEmptyString,
14} from "@t3tools/contracts";
15import * as Schema from "effect/Schema";
16
17export interface PreviewAutomationOperationContext {
18 readonly requestId: PreviewAutomationRequest["requestId"];
19 readonly operation: PreviewAutomationRequest["operation"];
20 readonly environmentId: PreviewAutomationHost["environmentId"];
21 readonly threadId: PreviewAutomationRequest["threadId"];
22 readonly tabId: Exclude<PreviewAutomationRequest["tabId"], undefined> | null;
23}
24
25export class PreviewAutomationOverlayTimeoutError extends Schema.TaggedError<PreviewAutomationOverlayTimeoutError>()(
26 "PreviewAutomationOverlayTimeoutError",
27 {
28 requestId: TrimmedNonEmptyString,
29 environmentId: EnvironmentId,
30 threadId: ThreadId,
31 timeoutMs: Schema.Int,
32 },
33) {
34 get responseTag() {
35 return "PreviewAutomationTimeoutError" as const;
36 }
37
38 override get message(): string {
39 return `Preview webview for request ${this.requestId} on environment ${this.environmentId} thread ${this.threadId} did not register within ${this.timeoutMs}ms.`;
40 }
41}
42
43export class PreviewAutomationNavigationTimeoutError extends Schema.TaggedError<PreviewAutomationNavigationTimeoutError>()(
44 "PreviewAutomationNavigationTimeoutError",
45 {
46 requestId: TrimmedNonEmptyString,
47 environmentId: EnvironmentId,
48 threadId: ThreadId,
49 tabId: PreviewTabId,
50 readiness: Schema.Literals(["domContentLoaded", "load"]),
51 timeoutMs: Schema.Int,
52 },
53) {
54 get responseTag() {
55 return "PreviewAutomationTimeoutError" as const;
56 }
57
58 override get message(): string {
59 return `Preview navigation for request ${this.requestId} on environment ${this.environmentId} thread ${this.threadId} tab ${this.tabId} did not reach ${this.readiness} readiness within ${this.timeoutMs}ms.`;
60 }
61}
62
63export class PreviewAutomationViewportTimeoutError extends Schema.TaggedError<PreviewAutomationViewportTimeoutError>()(
64 "PreviewAutomationViewportTimeoutError",
65 {
66 requestId: TrimmedNonEmptyString,
67 environmentId: EnvironmentId,
68 threadId: ThreadId,
69 tabId: PreviewTabId,
70 timeoutMs: Schema.Int,
71 },
72) {
73 get responseTag() {
74 return "PreviewAutomationTimeoutError" as const;
75 }
76
77 override get message(): string {
78 return `Preview viewport for request ${this.requestId} on environment ${this.environmentId} thread ${this.threadId} tab ${this.tabId} was not rendered within ${this.timeoutMs}ms.`;
79 }
80}
81
82export class PreviewAutomationTargetUnavailableError extends Schema.TaggedError<PreviewAutomationTargetUnavailableError>()(
83 "PreviewAutomationTargetUnavailableError",
84 {
85 requestId: TrimmedNonEmptyString,
86 operation: PreviewAutomationOperation,
87 environmentId: EnvironmentId,
88 threadId: ThreadId,
89 tabId: Schema.NullOr(PreviewTabId),
90 bridgeAvailable: Schema.Boolean,
91 },
92) {
93 get responseTag() {
94 return "PreviewAutomationTabNotFoundError" as const;
95 }
96
97 override get message(): string {
98 return `Preview automation target for ${this.operation} request ${this.requestId} is unavailable on environment ${this.environmentId} thread ${this.threadId} (tab ${this.tabId ?? "unassigned"}, bridge ${this.bridgeAvailable ? "available" : "unavailable"}).`;
99 }
100}
101
102export class PreviewAutomationRecordingNotActiveError extends Schema.TaggedError<PreviewAutomationRecordingNotActiveError>()(
103 "PreviewAutomationRecordingNotActiveError",
104 {
105 requestId: TrimmedNonEmptyString,
106 environmentId: EnvironmentId,
107 threadId: ThreadId,
108 tabId: Schema.NullOr(PreviewTabId),
109 },
110) {
111 get responseTag() {
112 return "PreviewAutomationExecutionError" as const;
113 }
114
115 override get message(): string {
116 return `Preview automation request ${this.requestId} found no active recording for tab ${this.tabId ?? "unassigned"} on environment ${this.environmentId} thread ${this.threadId}.`;
117 }
118}
119
120export class PreviewAutomationTargetNotEditableHostError extends Schema.TaggedError<PreviewAutomationTargetNotEditableHostError>()(
121 "PreviewAutomationTargetNotEditableHostError",
122 {
123 requestId: TrimmedNonEmptyString,
124 operation: PreviewAutomationOperation,
125 environmentId: EnvironmentId,
126 threadId: ThreadId,
127 tabId: Schema.NullOr(PreviewTabId),
128 selectorKind: Schema.optional(Schema.Literals(["focused-element", "locator", "selector"])),
129 selectorLength: Schema.optional(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))),
130 },
131) {
132 get responseTag() {
133 return "PreviewAutomationTargetNotEditableError" as const;
134 }
135
136 override get message(): string {
137 return `Preview automation ${this.operation} request ${this.requestId} requires an editable target in tab ${this.tabId ?? "unassigned"}.`;
138 }
139}
140
141const targetNotEditableDiagnostics = (
142 cause: unknown,
143): {
144 readonly selectorKind?: "focused-element" | "locator" | "selector";
145 readonly selectorLength?: number;
146} | null => {
147 if (
148 typeof cause !== "object" ||
149 cause === null ||
150 !("_tag" in cause) ||
151 cause._tag !== "PreviewAutomationTargetNotEditableError"
152 ) {
153 return null;
154 }
155 const selectorKind =
156 "selectorKind" in cause &&
157 (cause.selectorKind === "focused-element" ||
158 cause.selectorKind === "locator" ||
159 cause.selectorKind === "selector")
160 ? cause.selectorKind
161 : undefined;
162 const selectorLength =
163 "selectorLength" in cause &&
164 typeof cause.selectorLength === "number" &&
165 Number.isInteger(cause.selectorLength) &&
166 cause.selectorLength >= 0
167 ? cause.selectorLength
168 : undefined;
169 return {
170 ...(selectorKind === undefined ? {} : { selectorKind }),
171 ...(selectorLength === undefined ? {} : { selectorLength }),
172 };
173};
174
175export class PreviewAutomationOperationError extends Schema.TaggedError<PreviewAutomationOperationError>()(
176 "PreviewAutomationOperationError",
177 {
178 requestId: TrimmedNonEmptyString,
179 operation: PreviewAutomationOperation,
180 environmentId: EnvironmentId,
181 threadId: ThreadId,
182 tabId: Schema.NullOr(PreviewTabId),
183 cause: Schema.Defect(),
184 },
185) {
186 static fromCause(
187 input: PreviewAutomationOperationContext & { readonly cause: unknown },
188 ): PreviewAutomationHostError {
189 if (isPreviewAutomationHostError(input.cause)) return input.cause;
190 const diagnostics = targetNotEditableDiagnostics(input.cause);
191 return diagnostics
192 ? new PreviewAutomationTargetNotEditableHostError({
193 requestId: input.requestId,
194 operation: input.operation,
195 environmentId: input.environmentId,
196 threadId: input.threadId,
197 tabId: input.tabId,
198 ...diagnostics,
199 })
200 : new PreviewAutomationOperationError(input);
201 }
202
203 get responseTag() {
204 return "PreviewAutomationExecutionError" as const;
205 }
206
207 override get message(): string {
208 return `Preview automation ${this.operation} request ${this.requestId} failed on environment ${this.environmentId} thread ${this.threadId} (tab ${this.tabId ?? "unassigned"}).`;
209 }
210}
211
212export const PreviewAutomationHostError = Schema.Union([
213 PreviewAutomationRecordingTransferError,
214 PreviewAutomationRecordingDesktopUpdateRequiredError,
215 PreviewAutomationRecordingTooLargeError,
216 PreviewAutomationRecordingDeadlineExpiredError,
217 PreviewAutomationOverlayTimeoutError,
218 PreviewAutomationNavigationTimeoutError,
219 PreviewAutomationViewportTimeoutError,
220 PreviewAutomationTargetUnavailableError,
221 PreviewAutomationRecordingNotActiveError,
222 PreviewAutomationTargetNotEditableHostError,
223 PreviewAutomationOperationError,
224]);
225export type PreviewAutomationHostError = typeof PreviewAutomationHostError.Type;
226
227const isPreviewAutomationHostError = Schema.is(PreviewAutomationHostError);
228
229export function serializePreviewAutomationHostError(
230 error: PreviewAutomationHostError,
231): NonNullable<PreviewAutomationResponse["error"]> {
232 const detail = Object.fromEntries(
233 Object.entries(error).filter(
234 ([key]) =>
235 key !== "_tag" && key !== "cause" && key !== "name" && key !== "message" && key !== "stack",
236 ),
237 );
238 return {
239 _tag: "responseTag" in error ? error.responseTag : error._tag,
240 message: error.message,
241 ...(Object.keys(detail).length === 0 ? {} : { detail }),
242 };
243}